将 DataTable 导出到 Excel 文件的最佳方法是什么?
据我所知,有3种方法可以做到这一点:
- 第一种方法是使用COM Interop创建Excel文件,然后迭代所有单元格来读取和写入数据。创建的文件似乎是有效的(真正的.XLS文件),但似乎必须安装Excel才能使用这种方式
- 第二种方法是编写文件头(CSS样式),然后也迭代所有单元格。这种方式不需要安装Excel,但创建的文件可能无效(实际上是一个HTML文件)
- 第三种方式是将DataTable填充到DataGrid中,然后将其渲染为HTML文件,然后将其重命名为.xls扩大。
第三种方式似乎最简单,但不知道有没有更好的方式来存档?如果不是的话,这三种方式哪个最好?
太感谢了。
As far as I know, there're 3 ways to do this:
- The first way is using COM Interop to create a Excel file, then iterate over all cells to read and write data. The created file appears to be valid (true .XLS file), but it seems that Excel must installed to use this way
- The second way is writing a file header (CSS style), and then iterate over all cells, too. This way doesn't requires Excel installed, but the file created may not be valid (it's a HTML file, actually)
- The third way is fill DataTable into a DataGrid, and then render it into a HTML file, then rename it into .xls extension.
The third way seems to be simplest, but I don't know if there is a better way to archive this? If not, which is the best in these three ways?
Thank you so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第四种方法是使用 OleDbConnection 并使用标准 ADO.NET 将 .NET 应用程序连接到 Excel。
几年前我不得不做类似的事情,这篇文章非常有帮助。
http://davidhayden.com/blog/dave/archive/ 2006/05/26/2973.aspx
这种方法有很多奇怪的事情(主要是引用表和范围),但它最终只是一个 ADO.NET 实现,并且最终看起来非常像正常的ADO.NET 项目。
希望这有帮助!
A fourth way would be to use an OleDbConnection and connect your .NET application to Excel using standard ADO.NET.
I had to do something similar a few years back and this article was very helpful.
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
There's a lot of quirky things with this approach (mainly referencing tables and ranges), but it ends up being just an ADO.NET implementation and ends up looking very much like a normal ADO.NET project.
Hope this helps!
最简单的方法:CSV。只需对每个 DataRow 的每个值进行 ToString() 并将其放入文件中即可。您还可以将列名称拖放到文件中的第一行。 CSV 文件可以轻松导入到 Excel 中。
我的替代方案是使用 Codeplex 上的众多项目之一导出到 xslx。我玩过 ExcelPackage 和 简单的 OOXML,两者(IIRC)都可以正常工作。
您可以通过在 Codeplex 上搜索 Excel 或 OOXML 来找到更多信息。
Simplest way: CSV. Just ToString() on each value of each DataRow and drop it to a file. You can also drop the column names as the first row in the file. CSV files are imported easily into excel.
My alternate would be to export to xslx using one of the many projects on Codeplex. I've played with ExcelPackage and Simple OOXML, both of which (IIRC) work fine.
You can find more by searching for Excel or OOXML on Codeplex.
写入 CSV 时要小心,因为 Excel 会自动转换数据类型。例如,0001 将转换为数字 1。
另一个选择是使用 DataTable。 WriteXML(filename) 并将文件名设置为
something.xls
这不是二进制 XLS 文件,但也应该使用 Excel 打开。
顺便说一句:如果您需要格式化,则 HTML 表格方法会更好,并且对于大型表格来说,这两种方法都可能非常慢。
Be wary of writing to CSVs as Excel will automatically convert types of data. For example 0001 will be converted to a numerical 1.
Another option is to use DataTable.WriteXML(filename) and set the filename to
something.xls
This is not binary XLS file, but should open with Excel just as well.
BTW: The HTML table approach is better if you need formatting, and both of these approaches can be very slow for large tables.