VB.NET 导出数据到EXCEL中

类别:.NET开发 点击:0 评论:0 推荐:

----------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,有什么写得不对的,高手请指点了

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

本文地址:http://com.8s8s.com/it/it42123.htm