C# - 如何以编程方式添加 Excel 工作表 - Office XP / 2003
我刚刚开始通过 C# 摆弄 Excel,以便能够自动创建和添加到 Excel 文件。
我可以打开文件并更新其数据并浏览现有工作表。 我的问题是如何添加新工作表?
我尝试过:
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)excelApp.ThisWorkbook.Worksheets.Add(
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
但是我得到了COM异常,并且我的谷歌搜索没有给我任何答案。
HRESULT 异常:0x800A03EC 来源为:“Interop.Excel”
我希望有人能够让我摆脱痛苦。
I am just starting to fiddle with Excel via C# to be able to automate the creation, and addition to an Excel file.
I can open the file and update its data and move through the existing worksheets. My problem is how can I add new sheets?
I tried:
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)excelApp.ThisWorkbook.Worksheets.Add(
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
But I get below COM Exception and my googling has not given me any answer.
Exception from HRESULT: 0x800A03EC Source is: "Interop.Excel"
I am hoping someone maybe able to put me out of my misery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您需要将项目中的 COM 引用添加到 “
Microsoft Excel 11.0 对象库
” - 或任何合适的版本。这段代码对我有用:
在研究这个问题时我发现一些其他有用的链接:
根据 MSDN
希望有帮助。
You need to add a COM reference in your project to the "
Microsoft Excel 11.0 Object Library
" - or whatever version is appropriate.This code works for me:
Some other links that I found useful when looking into this question:
According to MSDN
Hope that helps.
感谢您的一些精彩回复。 @AR.,你是一个明星,它工作得很好。 昨晚我注意到
Excel.exe
没有关闭; 所以我做了一些研究并找到了如何释放 COM 对象。 这是我的最终代码:感谢您的帮助。
Would like to thank you for some excellent replies. @AR., your a star and it works perfectly. I had noticed last night that the
Excel.exe
was not closing; so I did some research and found out about how to release the COM objects. Here is my final code:Thank you for all your help.
AR 的另一个“Up Tick”...,但如果你不必使用互操作,我会完全避免它。 这个产品其实很有趣:
http://www.clearoffice.com/ 它提供了一个非常直观、完全托管的 API 进行操作excel 文件,似乎是免费的。 (至少目前)SpreadSheetGear 也很出色,但价格昂贵。
我的两分钱。
Another "Up Tick" for AR..., but if you don't have to use interop I would avoid it altogether. This product is actually quite interesting:
http://www.clearoffice.com/ and it provides a very intuitive, fully managed, api for manipulation excel files and seems to be free. (at least for the time being) SpreadSheetGear is also excellent but pricey.
my two cents.
您可以使用 OLEDB 创建和操作 Excel 文件。 请参阅此问题获取链接和示例。
You can use OLEDB to create and manipulate Excel files. See this question for links and samples.
以下是我发现的几件事:
您不能同时打开同一对象的多个实例。 例如,如果您实例化一个名为
xlsheet1
的新 Excel 工作表对象,则必须在创建另一个 Excel 工作表对象(例如xlsheet2
)之前释放它。 看起来 COM 失去了对该对象的跟踪,并在服务器上留下了一个僵尸进程。如果有多个用户访问同一文件,则使用与
excel.workbooks
关联的 open 方法也将变得难以关闭。 请改用 Add 方法,它在不锁定文件的情况下同样有效。 例如。xlBook = xlBooks.Add("C:\location\XlTemplate.xls")
释放 COM 对象后,将垃圾收集放在单独的块或方法中。
Here are a couple things I figured out:
You can't open more than one instance of the same object at the same time. For Example if you instanciate a new excel sheet object called
xlsheet1
you have to release it before creating another excel sheet object exxlsheet2
. It seem as COM looses track of the object and leaves a zombie process on the server.Using the open method associated with
excel.workbooks
also becomes difficult to close if you have multiple users accessing the same file. Use the Add method instead, it works just as good without locking the file. eg.xlBook = xlBooks.Add("C:\location\XlTemplate.xls")
Place your garbage collection in a separate block or method after releasing the COM objects.
COM 绝对不是一个好方法。 更具体地说,如果您正在处理 Web 环境,那么这是不行的...
我成功地使用了以下开源项目:
ExcelPackage for OOXML 格式 (Office 2007)
.XLS 格式的 NPOI (Office 2003)
查看这些博客文章:
在 C# 中创建 Excel 电子表格 .XLS 和 .XLSX
带有 Excel 表格和动态图表的 NPOI
COM is definitely not a good way to go. More specifically, it's a no go if you're dealing with web environment...
I've used with success the following open source projects:
ExcelPackage for OOXML formats (Office 2007)
NPOI for .XLS format (Office 2003)
Take a look at these blog posts:
Creating Excel spreadsheets .XLS and .XLSX in C#
NPOI with Excel Table and dynamic Chart
这就是我用来添加附加工作表的内容
This is what i used to add addtional worksheet
我在 VSTO 中的应用程序级加载项中遇到了类似的问题,添加新工作表时出现异常 HRESULT: 0x800A03EC。
Dominic Zukiewicz @ Excel 错误 HRESULT:0x800A03EC在尝试使用单元格名称获取范围时
然后我终于意识到 ThisWorkbook 触发了异常。 ActiveWorkbook 一切顺利。
I had a similar problem application-level add-in in VSTO, the exception HRESULT: 0x800A03EC when adding new sheet.
Dominic Zukiewicz @ Excel error HRESULT: 0x800A03EC while trying to get range with cell's name
Then I finally realized ThisWorkbook triggered the exception. ActiveWorkbook went OK.