----------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,有什麼寫得不對的,高手請指點了
--------------完-------------------------------