本文介绍了如何在VB.NET中将DataGridView导出为Excel格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用OLE使用VB.NET连接到数据库,并在DataGridView中显示结果。我想将DataGridView中的数据导出到Excel格式文件,即,用户可以将DataGridView的内容保存为MS Excel文件。
解决方案
我发现那个copyfromrecordset是最快的方法。
Dim xlApp As New Excel.Application
Dim xlWBook As Excel.Workbook = xlApp .Workbooks.Add
Dim XlSheet作为Excel.Worksheet = CType(xlWBook.Worksheets(Sheet1),Excel.Worksheet)
与XlSheet
'插入列名称
对于我= 2到dt.Columns.Count - 1
.Cells(1,i).value = dt.Columns(i - 1).ColumnName
下一个
'插入实际数据
.Range(A2)。CopyFromRecordset(datset)
结束
I'm using OLE to connect to a database using VB.NET, and show the results in a DataGridView.
I want to export the data that is in the DataGridView to an Excel format file, i.e., the user can save the content of the DataGridView as MS Excel file.
解决方案
I found that copyfromrecordset is the fastest way.
Dim xlApp As New Excel.Application
Dim xlWBook As Excel.Workbook = xlApp.Workbooks.Add
Dim XlSheet As Excel.Worksheet = CType(xlWBook.Worksheets("Sheet1"), Excel.Worksheet)
With XlSheet
'insert column names
For i = 2 To dt.Columns.Count - 1
.Cells(1, i).value = dt.Columns(i - 1).ColumnName
Next
'insert the actual data
.Range("A2").CopyFromRecordset(datset)
End With
这篇关于如何在VB.NET中将DataGridView导出为Excel格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!