在 C# 中访问打开的 Excel 工作簿
我需要访问一个已经打开的 Excel 文件。我以为只要检查 .Workbooks
属性,它就会在那里,但事实并非如此。获取对打开工作簿的引用的正确方法是什么?
var app = new Microsoft.Office.Interop.Excel.Application();
// the count is 0 =(
app.Workbooks.Count == 0;
编辑
我可以通过...获取对Excel应用程序的引用...
app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
但是app.Workbooks.Count
仍然是0
为什么它不能获取对打开的工作簿的引用?
I need to access an excel file that is already open. I thought just inspecting the .Workbooks
property that it would be there but it isn't. What is the right way to get a reference to the open workbook?
var app = new Microsoft.Office.Interop.Excel.Application();
// the count is 0 =(
app.Workbooks.Count == 0;
EDIT
I can get a reference to the Excel Application via...
app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
but app.Workbooks.Count
is still 0
why isn't it able to get a reference to the opened workbook?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
添加到 Armbrat 的解决方案中;这对我有用:
To add to Armbrat's solution; this worked for me:
我知道它的老问题,但我只是发布我的解决方案
我的解决方案是
我希望它有帮助
i know its old question but i just post my solution
my solution was
i hope it helpful
检查现有实例,而不是实例化新实例:
Instead of instantiating a new instance, check for an existing one:
试试这个代码:
Try this code: