使用标题文本导出到Excel

时间:2017-02-20 13:11:17

标签: excel vb.net datagridview export headertext

我的代码应该在导出到Excel时包含标题文本。

For i As Integer = 0 To DataGridView2.Rows.Count - 2
    For j As Integer = 0 To DataGridView2.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) = DataGridView2.Columns(j).HeaderText
        Else
            worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView2.Rows(i).Cells(j).Value
        End If
        cellColumnIndex += 1
    Next
    cellColumnIndex = 1
    cellRowIndex += 1
Next

但是,此代码将第一个数据行替换为标题文本,而不是将其插入上面。如果我删除提取标题文本的If语句,我会将所有行都删除,但我不会获得标题文本。

For i As Integer = 0 To DataGridView2.Rows.Count - 2
    For j As Integer = 0 To DataGridView2.Columns.Count - 1
        worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView2.Rows(i).Cells(j).Value
        cellColumnIndex += 1
    Next
    cellColumnIndex = 1
    cellRowIndex += 1
Next

关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:2)

下面的代码创建一个带有DataGridView标头的Excel文件。我在Visual Studio 2010中测试过它。首先,您必须添加Microsoft Office程序集的引用。

  • Microsoft.Office.Interop.Excel(Version - 12.0.0.0)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer
    
    xlApp = New Microsoft.Office.Interop.Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")
    
    For i = 0 To DataGridView1.RowCount - 2
        For j = 0 To DataGridView1.ColumnCount - 1
            For k As Integer = 1 To DataGridView1.Columns.Count
                xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
            Next
        Next
    Next
    
    xlWorkSheet.SaveAs("C:\vbToexcel.xlsx")
    xlWorkBook.Close()
    xlApp.Quit()
    
    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)
    
    MsgBox("File successfully created - C:\vbToexcel.xlsx")   End Sub
    
    Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try   End Sub
    

答案 1 :(得分:1)

在跟踪代码后,很明显您在两个for循环中遇到了索引问题。您提供的代码似乎缺少第一行数据。

正如你评论的那样:

  

此代码用标题文本替换第一个数据行,而不是将其插入 ...

这是不正确的,它不是替换它只是跳过DataGridView中第一行数据的行。以下是您要解释的代码。

For i As Integer = 0 To DataGridView1.Rows.Count - 2
  For j As Integer = 0 To DataGridView1.Columns.Count - 1
    If cellRowIndex = 1 Then
      worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView1.Columns(j).HeaderText
    Else
      worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView1.Rows(i).Cells(j).Value
    End If
    cellColumnIndex += 1
  Next
  cellColumnIndex = 1
  cellRowIndex += 1
Next

基本上,这会遍历行,然后是列。问题出在If语句和索引i中。在这个If语句中,您检查这是否是第一次获取标题。如果是第一次将标题写入excel并继续。这将跳过第一行数据,因为循环变量i被用作具有赋值的DataGridView行的索引:

worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView1.Rows(i).Cells(j).Value

进入j循环时,i周围的第一次为零(0)。使用cellRowIndex进行检查以确定是否需要输出标头。在这种情况下,他们会...输出标题然后退出此if并循环回到下一个标题。输出所有标头后,退出j循环并循环回i循环。这会将i增加到1并输入j循环...由于i在输出标题时已经为0,我们将跳过/错过DataGridView中的第0行。我希望这是有道理的。

您所拥有的简单解决方案就是在-1处使用以下内容启动i

For i As Integer = -1 To DataGridView1.Rows.Count - 2

这将解决您遇到的问题,但代码不易遵循。我建议使用foreach循环来循环DataGridView行并将列输出与行输出分开。这确实创建了两个循环,但第一个循环只会循环一次以添加标题。下一个循环遍历所有行。这将使索引更容易处理,并且将来更容易阅读。

For Each column In DataGridView1.Columns
  worksheet.Cells(1, column.Index + 1).Value = column.Name
Next

Dim rowIndex = 2
For Each row As DataGridViewRow In DataGridView1.Rows
  If Not row.IsNewRow Then
    For colIndex As Integer = 0 To DataGridView1.Columns.Count - 1
      worksheet.Cells(rowIndex, colIndex + 1).Value = row.Cells(colIndex).Value.ToString
    Next
  End If
  rowIndex += 1
Next

希望这有帮助。