2014-12-04 144 views
1

我想從數據庫生成一個Excel表(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工作表,但它只顯示一行(中間名)。我有兩個值名和中間名。我怎麼能實現它?

+0

讓表單中的VB宏訪問數據庫並獲取信息更聰明。它可能在Excel 2000-2003中,但不確定最新版本。 – i486 2014-12-04 10:52:55

+0

@varchar,compnay名稱未合併顯示在第一列第一行 – 2014-12-04 11:22:50

+0

註釋不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/66198/discussion-on-question-by-sara-john-how-to-bring-data-from-database-to- Excel的她)。 – Taryn 2014-12-04 11:29:06

回答

1
foreach (DataRow row in rows) 
{ 
    string name = Convert.ToString(row["Fname"]); 
    string code = Convert.ToString(row["Middle Name"]); 

    // Setting Value in cell 
    ws.Cells[colIndex, rowIndex].Value = name; 
    ws.Cells[colIndex + 1, rowIndex].Value = code;  

    // Move to the next row in the sheet. 
    rowIndex++; 
} 
+0

(row [..]);你的意思是? – 2014-12-04 09:42:39

+0

@SaraJohn例如,如果你想從第一列中獲得值,你可以使用'row [0]'。 – mainvoid 2014-12-04 09:49:32

+0

ws.Cells [1,1,dt.Columns.Count] .Merge = true;字段顯示列超出範圍的錯誤消息。爲什麼? – 2014-12-04 09:52:51

相關問題