如何将数据库表转储到Excel表?

发布于 2024-11-26 12:10:02 字数 912 浏览 0 评论 0原文

我正在从数据库中获取数据,并且希望将该数据作为 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 ?

请帮忙..我是新手!

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.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()

There should be a straight obvious way of doing this ?

Dumping a database table to excel sheet ?

[ Should I ? ]

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

Please help.. I am new to this !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的那么颓废 2024-12-03 12:10:02

这是我解决这个问题的方法:

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

最后,对您想要获取此数据的范围执行以下操作

range.Value = getDate(query,conStr)

这解决了整个问题!

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 !

唠甜嗑 2024-12-03 12:10:02

最简单的解决方案是编写 csv 文件。在 csv 中,各列用逗号 (;) 分隔,excel 可以读取该文件。

最好的方法是 OLE 自动化,但为此您必须在运行程序的电脑上安装 Excel。
这是一个示例:OLE 自动化

The simplest solution is if you write a csv file. In the csv the columns are separated with comma (;) and the excel can read this file.

The best way is the OLE Automation but for this you have to install an excel to the pc where your program runs.
Here is a sample: OLE Automation

甜中书 2024-12-03 12:10:02

最好的方法是使用数据表的 XmlSerialization,然后执行序列化 Xml 的 Excel 加载...

有一个很好的开源实用程序(您可以将其添加到项目中并进行更改),称为 ExcelLibrary ...请请参阅这篇文章... 创建 Excel(.XLS 和 .XLSX)文件来自C#

这是批量加载 Excel 的最有效方法之一。

Best way is to use XmlSerialization of the data table and then perform Excel Loading of the serialised Xml...

There is a nice open source utility (which you can add to your project and make changes to as well) called ExcelLibrary ... please refer to this article... Create Excel (.XLS and .XLSX) file from C#

This is one of the most efficient ways of Bulk Excel Loading.

乙白 2024-12-03 12:10:02

看看像 EPlus 这样的东西,它似乎是一个流行的库。

...或FileHelpers,那里有一个很好的快速入门 向您展示如何创建 .csv 文件

如果这是一次性作业,您可以只使用 Sql Server Management studio 的导出功能吗?

Look at something like EPlus which seems to be a popular library.

... or FileHelpers, where there is a good quickstart showing you how to create a .csv file

You can just use the export functionality of Sql Server Management studio if it's a one off type job?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文