2017-11-25 195 views
0

標題說明了一切。還從微軟獲得了代碼。它跳過標題和最後一行之後的第一行,但成功導出爲ex​​cel。Visual Studio 2015 datagridview到excel似乎跳過2行。代碼有什麼問題?

Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application() 
     Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing) 
     Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing 

     Try 

      worksheet = workbook.ActiveSheet 

      worksheet.Name = "ExportedFromDatGrid" 

      Dim cellRowIndex As Integer = 1 
      Dim cellColumnIndex As Integer = 1 

      'Loop through each row and read value from each column. 
      For i As Integer = 0 To DataGridView1.Rows.Count - 2 
       For j As Integer = 0 To DataGridView1.Columns.Count - 1 
        ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check. 
        If cellRowIndex = 1 Then 
         worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView1.Columns(j).HeaderText 
        Else 
         worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView1.Rows(i).Cells(j).Value.ToString() 
        End If 
        cellColumnIndex += 1 
       Next 
       cellColumnIndex = 1 
       cellRowIndex += 1 
      Next 

      'Getting the location and file name of the excel to save from user. 
      Dim saveDialog As New SaveFileDialog() 
      saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*" 
      saveDialog.FilterIndex = 2 

      If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
       workbook.SaveAs(saveDialog.FileName) 
       MessageBox.Show("Export Successful") 
      End If 
     Catch ex As System.Exception 
      MessageBox.Show(ex.Message) 
     Finally 
      excel.Quit() 
      workbook = Nothing 
      excel = Nothing 
     End Try 

感謝您的幫助!這是非常讚賞。我無話可說。請讓我發佈我的問題堆棧。

+0

你設置一個斷點,單步執行代碼?如果沒有,那麼你需要這樣做。這樣做,您會發現代碼中的行爲與您的期望不符。如果你不這樣做,那麼這意味着你的期望首先是錯誤的。 – jmcilhinney

+0

得到它,因爲cellrowindex = 1的值。它寫入標題和cellrowindex = 2。它從第二行開始並跳過第一行。以及它跳過的最後一行。我只是將dgv.rows.count -2更改爲dgv.rows.count -1 – Vainicz

回答

0

你的循環是負責這個問題(如你在你的評論中寫的)。
爲什麼不把它寫得更具可讀性和笨拙?

這裏是我的建議:

Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application() 
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing) 
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing 

Try 

    worksheet = workbook.ActiveSheet 

    worksheet.Name = "ExportedFromDatGrid" 


    For cellColumnIndex = 0 To dgv.Columns.Count - 1 
     worksheet.Cells(1, cellColumnIndex + 1) = dgv.Columns(cellColumnIndex).HeaderText 
     For cellRowIndex = 0 To dgv.RowCount - 1 
      worksheet.Cells(cellRowIndex + 2, cellColumnIndex + 1) = dgv.Rows(cellRowIndex).Cells(cellColumnIndex).Value 
     Next 
    Next 


    worksheet.Rows("1:10000").RowHeight = 25 
    'Getting the location and file name of the excel to save from user. 
    Dim saveDialog As New SaveFileDialog() 
    saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*" 
    saveDialog.FilterIndex = 2 

    If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
     workbook.SaveAs(saveDialog.FileName) 
     MessageBox.Show("Export Successful") 
    End If 
Catch ex As System.Exception 
    MessageBox.Show(ex.Message) 
Finally 
    excel.Quit() 
    workbook = Nothing 
    excel = Nothing 
End Try 
相關問題