我从我的数据库中获取数据,我希望将这些数据作为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)
这解决了整个问题!