怎么样使用VB.NET导出数据到EXCEL中?
----------VB.NET 导出数据到EXCEL中-------------
在引用当中引入Microsoft Excel 11.0 Object Library
Dim oExcel As New Excel.Application
Dim obook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim DataArray(2000, 2000) As Integer
obook = oExcel.Workbooks.Open("c:/1.xls")
oSheet = obook.Worksheets(1)
oSheet.Name = "ok"
Dim myAdapter As New OleDb.OleDbDataAdapter("select * from list", Module1.dbConn)
Dim mydataset As New DataSet
myAdapter.Fill(mydataset, "list")
DataGrid1.DataSource = mydataset.Tables("list")
Dim i As Integer
Dim iCount As Integer
iCount = mydataset.Tables("list").Rows.Count
For i = 0 To iCount - 1
DataArray(i, 0) = DataGrid1.Item(i, 0)
Next
oSheet.Range("b1").Resize(100, 1).Value = DataArray
obook.Save()
oExcel.Quit()
添加数据到EXCEL的行(列),源数据的数组要与EXCEL的行(列)对应。
上面代码使用的是DataGrid的控件,如果使用Table也是一样一样的哦
数组大小可以自己来定义,如果数据的数量大于数组的范围,那么以数组为准(谁叫要用数组输出呢 :-))
BTW:机器要安装了office才能够引用Microsoft Excel 11.0 Object Library
路径是*/Microsoft office/office11/execl.exe
第一次写blog,有什么写得不对的,高手请指点了
--------------完-------------------------------
本文地址:http://www.45fan.com/bcdm/69141.html