How dump database table to excel sheet?

后端 未结 4 587
一整个雨季
一整个雨季 2020-12-21 20:16

I am getting data from my database and I want to have that data as a table in excel file. So, I have written the following :

    Dim sheetToPopulate As Excel         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 20:38

    Here is how I solved this :

    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)
    

    That solved the whole problem !

提交回复
热议问题