用标题文本导出到Excel

我有导出到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 

但是,这个代码将第一个数据行replace为标题文本,而不是将其插入到上面。 如果我删除了提取标题文本的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 

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

跟踪你的代码后,很明显你在两个for循环中有一个索引问题。 看来您提供的代码缺less第一行数据。

正如你所评论的:

此代码replace第一个数据行的标题文本,而不是将其插入上面

这是不正确的,它不是取代行,它只是简单地跳过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并继续。 这将跳过第一行数据,因为循环variablesi被用作具有赋值的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行。 我希望这是有道理的。

你有一个简单的解决scheme将是简单地开始i -1在:

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

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

 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 

希望这可以帮助。

下面的代码创build一个Excel文件与DataGridView标题。 我已经在Visual Studio 2010中对其进行了testing。首先,您必须添加Microsoft Office程序集的引用。

  • Microsoft.Office.Interop.Excel(版本 – 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