使用 C# 将数据插入 Excel(2007 和 2010)的最佳方法是什么
我当前的情况是,我有 20 个 Excel 文件,需要通过运行多个脚本来填充。目前这是一个手动过程。我即将启动一个小项目,该项目应该自动化大部分(如果不是全部)工作。
工具:
我目前使用的是 Excel 2007,但在不久的将来可能会使用 Excel 2010。我目前计划使用 VS 2005 Professionl 或 C# Express 2010。
我的方法是为 20 个文件创建模板,以便已保存格式和未更改的数据。然后,我计划通过 C# 在每个文件的每个“工作表”内的适当单元格插入数据。
我遇到过多种方法(此处和其他网站),即 -
- Excel Interop
- OLE DB
- ADO.NET
- Open XML
我试图找出是否有人以前做过类似的事情以及哪种技术最有效(如果可能的话,提供一些解释或链接以获取更多信息)。
如果问题太主观且不恰当,我很抱歉。如果有人决定关闭它,我会理解。
谢谢。
My current scenario is that I have 20 Excel files that need to be populated by running multiple scripts. Currently this is a manual process. I am about to start a small project that should automate most if not all of the work.
Tools:
I am currently using Excel 2007, but could potentially be working with Excel 2010 in the near future. I am currently planing to use VS 2005 Professionl or C# Express 2010.
My approach is to create templates for the 20 files, so that formatting and data that is not changing is already saved. Through C# I plan to then insert data at appropriate cells within each "Sheet" per file.
I have come across multiple approaches that I read on (here and other sites), namely -
- Excel Interop
- OLE DB
- ADO.NET
- Open XML
I am trying to find out if someone has done something similar before and which one of the technologies would work best (if possible with some explanation or link for more information).
I am sorry if the question is too subjective and not appropriate. I will understand if someone decides to close it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能会遗漏一些东西,但如果您谈论的是 C#,我认为 OLE DB 和 ADO.Net 最终是相同的。基本上,您将使用 Excel for ADO.Net 的 OLE DB 提供程序。
我建议使用 http://www.connectionstrings.com/ 了解如何设置您的提供商。
但是,如果您要使用模板,则非常简单。只需在 Excel 文件中设置“表格”即可。确保使用命名范围。剩下的就是编写一些基本的 SQL,例如:
就是这样。我偶尔用它来处理数据。我确实更频繁地使用它来读取数据。
问候,
埃里克。
I might be missing something, but I would think OLE DB and ADO.Net end up being the same if you are talking C#. Basically, you would use the OLE DB provider for Excel for ADO.Net.
I recommend http://www.connectionstrings.com/ for how to setup your provider.
However, if you are going to use templates, it's pretty easy. Just setup your "tables" in your Excel files. Make sure you use named ranges. All that is left is to write some basic SQL like:
And that is it. I have used it occasionally for data manipulation. I do use it far more often for data reading.
Regards,
Eric.
我认为 OLE DB 是实现此目的最简单的方法。
I think OLE DB is the easyest way to do this.
OLEDB
对我来说是最好的方法。OLEDB
is for me the best way.