2013-06-24 40 views
0

enter image description hereenter image description here我有一個窗體,其中生成報表並在datagridview中顯示,當用戶單擊「導出到Excel」時,datagridview中的數據將轉移到Excel文件中。 但即時通訊有問題:在GridView的第一行不會被保存在Excel文件 請幫助將報表詳細信息導出到Excel文件時出錯

在此先感謝

下面

是即時通訊使用

 if (dgvCreditLimitTransaction.RowCount >= 1) 
     { 
      // creating Excel Application 
      Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
      // creating new WorkBook within Excel application 
      Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
      // creating new Excelsheet in workbook 
      Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
      // see the excel sheet behind the program 
      app.Visible = true; 
      // get the reference of first sheet. By default its name is Sheet1. 
      // store its reference to worksheet 
      worksheet = workbook.Sheets["Sheet1"]; 
      worksheet = workbook.ActiveSheet; 
      // changing the name of active sheet 
      worksheet.Name = "Transaction Details"; 
      // storing header part in Excel 
      int hdinvdate = 0; 
      for (int i = 2; i <= dgvCreditLimitTransaction.ColumnCount; i++) 
      { 
       worksheet.Cells[1, i] = dgvCreditLimitTransaction.Columns[i - 1].HeaderText; 
       if (dgvCreditLimitTransaction.Columns[i - 1].HeaderText == "LC Number") 
       { 
        hdinvdate = i - 1; 
       } 
      } 
      // storing Each row and column value to excel sheet 
      for (int i = 0; i < dgvCreditLimitTransaction.Rows.Count; i++) 
      { 
       for (int j = 1; j < dgvCreditLimitTransaction.ColumnCount; j++) 
       { 
        if (dgvCreditLimitTransaction.Rows[i].Cells[j].Value != null) 
        { 
         if (j == hdinvdate) 
         { 

          DateTime tempinvdt = Convert.ToDateTime(dgvCreditLimitTransaction.Rows[i].Cells[j].Value); 

          worksheet.Cells[i + 2, j + 1] = tempinvdt.ToString("MM/dd/yyyy"); 

         } 
         else 
         { 
          worksheet.Cells[i + 2, j + 1] = dgvCreditLimitTransaction.Rows[i].Cells[j].Value.ToString(); 
         } 

        } 
       } 
      } 
      // Exit from the application 
      //   app.Quit(); 
     } 
     else 
     { 
      MessageBox.Show("Please select Data"); 
     } 
+0

也許你能告訴什麼是在DGV VS您在Excel電子表格中得到什麼的截圖。另外,爲什麼你在列循環中使用j = 1,你的意思是將該列從excel文檔中刪除? – KreepN

+0

該代碼被刪除的第一行的DGV – Sam

+0

和在我的其他形式我想要的第一列也在Excel的 及其第一列,而不是第一列 – Sam

回答

0

代碼的修改代碼在下面,並註釋了評論。 IT是我的理解,你需要Excel電子表格中的第一列,但不是第一行。如果您需要進行任何調整,請留下評論,我會通過更新記錄。


//I changed i=2 to i = 1 to get first column header 
for (int i = 1; i <= dgvCreditLimitTransaction.ColumnCount; i++) 
{ 
    worksheet.Cells[1, i] = dgvCreditLimitTransaction.Columns[i - 1].HeaderText; 

    if (dgvCreditLimitTransaction.Columns[i - 1].HeaderText == "LC Number") 
    { 
     hdinvdate = i - 1; 
    } 
} 


//changed i = 0 to i = 1 to remove the first row, if you wish to keep the first row set i = 0 
for (int i = 0; i < dgvCreditLimitTransaction.RowCount; i++) 
{ 
    //change j = 1 to j = 0 so that you get 1st columns data 
    for (int j = 0; j < dgvCreditLimitTransaction.ColumnCount; j++) 
    { 
     if (dgvCreditLimitTransaction.Rows[i].Cells[j].Value != null) 
     { 
      if (j == hdinvdate) 
      { 

       DateTime tempinvdt = Convert.ToDateTime(dgvCreditLimitTransaction.Rows[i].Cells[j].Value); 

       worksheet.Cells[i + 2, j + 1] = tempinvdt.ToString("MM/dd/yyyy"); 

      } 
      else 
      { 
       worksheet.Cells[i + 2, j + 1] = dgvCreditLimitTransaction.Rows[i].Cells[j].Value.ToString(); 
      } 

     } 
    } 
} 
// Exit from the application 
//   app.Quit(); 
} 
else 
{ 
MessageBox.Show("Please select Data"); 
}