VB.NET導出數據到EXCEL中。本站提示廣大學習愛好者:(VB.NET導出數據到EXCEL中)文章只能為提供參考,不一定能成為您想要的結果。以下是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,有什麼寫得不對的,高手請指點了
--------------完-------------------------------