如何将数据库表转储到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 ?
请帮忙..我是新手!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我解决这个问题的方法:
这解决了整个问题!
Here is how I solved this :
That solved the whole problem !
最简单的解决方案是编写 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
最好的方法是使用数据表的 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.
看看像 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?