2013-06-24 4 views
0

enter image description hereenter image description here 보고서가 생성되어 Dataagridview에 표시되는 양식이 있습니다. 사용자가 'Excel로 내보내기'를 클릭하면 Dataagridview의 데이터가 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에 무엇이의 스크린 샷을 보여줄 수 있습니다. 또한, 왜 당신은 열 루프에서 j = 1을 사용하고 있습니까, 엑셀 문서에서 그 열을 남겨 두는 것을 의미합니까? – KreepN

+0

코드는 DGV – Sam

+0

의 첫 번째 줄을 지우도록 만들어졌고 다른 형태로도 첫 번째 열을 Excel에서도 사용하려고합니다 및 그 첫 번째 열이 아니라 첫 번째 줄을 찾아야합니다. – Sam

답변

0

변경을 사용하여 메신저 코드 아래에 있으며 주석이 적혀 있습니다. 첫 번째 행이 아니라 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"); 
} 
관련 문제