日期:2008-06-18  浏览次数:20585 次

----------VB.NET 导出数据到Excel中-------------

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也是一样一样的哦


数组大小可以自己来定义,如果数据的数量大于数组的范围,那么以数组为准(谁叫要用数组输出呢 :-))

第一次写blog,有什么写得不对的,高手请指点了

--------------完-------------------------------