本文主要是介绍VB.NET2003 DataGrid输出到Excel2003,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这是一个导出Excel文件的函数,在你的Button事件中调用它就行了。~
注意:一定要在工程中引用excel类型库。做法:从"工程"菜单中选择"引用"栏;选择microsoft excel 11.0 object library(excel2003),然后选择"确定"。
Dim myExcel As Excel.Application
Public Sub ExportToExcel(ByVal mytable As DataTable)
Try
Dim datav As New DataView
datav = mytable.DefaultView
Dim i, j As Integer
Dim rows As Integer = datav.Table.Rows.Count
Dim cols As Integer = datav.Table.Columns.Count
Dim DataArray(rows - 1, cols - 1) As String
myExcel = New Excel.Application
For i = 0 To rows - 1
For j = 0 To cols - 1
If datav.Table.Rows(i).Item(j) Is System.DBNull.Value Then
Else
DataArray(i, j) = datav.Table.Rows(i).Item(j)
End If
Next
Next
myExcel.Application.Workbooks.Add(True)
myExcel.Visible = True
For j = 0 To cols - 1
myExcel.Cells(1, j + 1) = datav.Table.Columns(j).ColumnName
Next
myExcel.Range("A2").Resize(rows, cols).Value = DataArray
myExcel.Quit()
Catch exp As Exception
MessageBox.Show("数据导出错误! 请查看您是否安装了Excel。")
End Try
End Sub
这篇关于VB.NET2003 DataGrid输出到Excel2003的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!