----------VB.NET 导出数据到EXCEL中-------------
在引用当中引入Microsoft Excel 11.0 Object Library
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
Dim oExcel As New Excel.ApplicationDim obook As Excel.WorkbookDim oSheet As Excel.WorksheetDim DataArray(2000, 2000) As Integerobook = 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 DataSetmyAdapter.Fill(mydataset, "list")DataGrid1.DataSource = mydataset.Tables("list")Dim i As IntegerDim iCount As IntegeriCount = mydataset.Tables("list").Rows.CountFor i = 0 To iCount - 1DataArray(i, 0) = DataGrid1.Item(i, 0)NextoSheet.Range("b1").Resize(100, 1).Value = DataArrayobook.Save()oExcel.Quit()(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
添加数据到EXCEL的行(列),源数据的数组要与EXCEL的行(列)对应。
上面代码使用的是DataGrid的控件,如果使用Table也是一样一样的哦
数组大小可以自己来定义,如果数据的数量大于数组的范围,那么以数组为准(谁叫要用数组输出呢 :-))
BTW:机器要安装了office才能够引用Microsoft Excel 11.0 Object Library
路径是*Microsoft officeoffice11execl.exe
第一次写blog,有什么写得不对的,高手请指点了
--------------完-------------------------------