.net – 如何将数据库表转储到Excel工作表?

前端之家收集整理的这篇文章主要介绍了.net – 如何将数据库表转储到Excel工作表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从我的数据库获取数据,我希望将这些数据作为excel文件中的表.
所以,我写了以下内容
Dim sheetToPopulate As Excel.Worksheet = getSheet()
    Dim reader As OleDbDataReader
    Dim query As String = "SELECT * FROM dataTable"
    Dim cmd As New OleDbCommand(query,oleConn)
    Dim reader As OleDbDataReader
    oleConn.Open()
    reader = cmd.ExecuteReader()
    Do While reader.Read()
        // How use the reader to populate the sheet at once.
        // I have the sheet object as sheetToPopulate.
        // cell.Vaule = reader.GetString(0)  ' It would be very in-efficient and complex.
        // How can I dump the table to my excel sheet ?
    Loop
    reader.Close()
    oleConn.Close()

应该有一个明显的方法来做到这一点?

数据库表转储到Excel工作表?

[ 我是不是该 ? ]

Should I use dataset of something.. ? If yes,how to proceed for that ?

请帮助..我是新来的!!

解决方法

以下是我解决这个问题的方法
Private Function getData(ByVal query As String,ByVal conStr As String) As Object
    Dim adapter As New Data.OleDb.OleDbDataAdapter(query,conStr)
    Dim dataSet As New Data.DataSet
    adapter.Fill(dataSet)
    Dim dataTable As Data.DataTable = dataSet.Tables(0)
    Dim data(dataTable.Rows.Count,dataTable.Columns.Count - 1) As Object
    For col = 0 To dataTable.Columns.Count - 1
        For row = 0 To dataTable.Rows.Count - 1
            data(row,col) = dataTable.Rows(row).ItemArray(col)
        Next
    Next
    Return data
End Function

Then Finally,do the following to the range where you want to have this data

range.Value = getDate(query,conStr)

解决了整个问题!

猜你在找的MsSQL相关文章