2017-11-25 251 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