1

データベース (sql 2008) から Excel シートを生成しようとしています。以下のコードを書きましたが、動作しません。私のコードを改善するのを手伝ってください。私のサンプルコードの下

protected void generate_Click(object sender, EventArgs e)
    { 
       DataTable dt = new DataTable(); //My Function which generates DataTable
        DataSet ds = new DataSet();
        using (ExcelPackage p = new ExcelPackage())
       {
            //Here setting some document properties
            p.Workbook.Properties.Author = "Zeeshan Umar";
            p.Workbook.Properties.Title = "Office Open XML Sample";

            //Create a sheet
            p.Workbook.Worksheets.Add("Sample WorkSheet");
            ExcelWorksheet ws = p.Workbook.Worksheets[1];
            ws.Name = "Sample Worksheet"; //Setting Sheet's name
            ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet
            ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet

            //Merging cells and create a center heading for out table
            ws.Cells[1, 1].Value = "msd";
            ws.Cells[1, 1, 1, ws.Dimension.End.Column].Merge = true;
           // ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true;
            ws.Cells[1, 1, 1, ws.Dimension.End.Column].Style.Font.Bold = true;
            ws.Cells[1, 1, 1, ws.Dimension.End.Column].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
              int colIndex = 1;
              int rowIndex = 2;
              SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

              SqlCommand cmd = new SqlCommand("SELECT Fname,Mname FROM EmpMaster where EmpCode='" + ddcode.SelectedItem.Text + "'", conn);
              // dr = conn.query(str);
              SqlDataAdapter adr = new SqlDataAdapter();

              adr.SelectCommand = cmd;
              adr.Fill(dt);
              //Add the table to the data set
              ds.Tables.Add(dt);
              //cell.Value = "Heading " + ds.ColumnName;
              var rows = ds.Tables[0].Rows;
               foreach (DataRow row in rows)
                     {
                          string name = Convert.ToString(row["Fname"]);
                         string code = Convert.ToString(row["Mname"]);
                         string lname = Convert.ToString(row["Lname"]);
                         //ws.Cells[colIndex +1 , rowIndex +0].Value = name;
                         //ws.Cells[colIndex +1, rowIndex  +1].Value = code;
                         //ws.Cells[colIndex +1, rowIndex +2].Value = lname;
                         ws.Cells[rowIndex , colIndex ].Value = name;
                         ws.Cells[rowIndex , colIndex +1 ].Value = code;
                         ws.Cells[rowIndex , colIndex +2].Value = lname;
                         // Move to the next row in the sheet.
                        rowIndex++;
                         colIndex++;

        }
               //Generate A File with Random name
              Byte[] bin = p.GetAsByteArray();
              string file = "F:\\ excelsample.xlsx";
              File.WriteAllBytes(file, bin);
              System.Diagnostics.Process.Start("F:\\ excelsample.xlsx");
        }
}

これにより Excel シートが生成されますが、1 行 (ミドル ネーム) しか表示されません。Firstname と Middle name の 2 つの値があります。どうすれば達成できますか?

4

1 に答える 1