你好,
可以考虑使用Interop.Excel 来做. 下面的代码你可以参考一下(导出部分代码):
首先要添加引用:Microsoft.Office.Interop.Excel
Dim MyExcel As New Microsoft.Office.Interop.Excel.Application()
MyExcel.Application.Workbooks.Add()
MyExcel.Visible = True
'获取标题
Dim Cols As Integer
For Cols = 1 To DataGridView1.Columns.Count
MyExcel.Cells(1, Cols) = DataGridView1.Columns(Cols - 1).HeaderText
Next
'往excel表里添加数据()
Dim i As Integer
For i = 0 To DataGridView1.RowCount - 1
Dim j As Integer
For j = 0 To DataGridView1.ColumnCount - 1
If Me.DataGridView1(j, i).Value Is System.DBNull.Value Then
MyExcel.Cells(i + 2, j + 1) = ""
Else
MyExcel.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString
End If
Next j
Next i
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.