使用 VSTO 访问 C# 类文件中的 Excel 工作表
我使用 VSTO 模板(VS2010、Excel2007)创建了一个 Excel 插件。 在解决方案资源管理器中,我有一个名为 Excel 的组,其下有一个名为 ExcelAddIn.cs 的文件。这可以通过诸如此类的代码访问活动工作表。
public partial class MyAddIn
{
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Application.Activesheet;
Excel.Range firstRow = activeWorksheet.get_Range("A1",missing);
}
该代码工作正常,即。我可以得到 Excel 模型。
然而,我不想将所有处理代码都放在这个类文件中,而是想在另一个类文件中处理 Excel 工作表数据。我已经创建了这个文件,但无法在其中使用上面的任何代码,即。我似乎无法从此文件访问 Excel 模型。 我复制了“使用 Microsoft.Office.Tools.Excel”引用,但放入类似以下的行:
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Application.Activesheet;
给我一个“名称“应用程序”在当前上下文中不存在”错误。
关于我需要进行哪些参考/更改才能从这个单独的类文件获取 Excel 模型,有什么想法吗?
顺便提一句。有效的文件引用第一个代码行的“Excel.Application”对象,第二个无效的单独文件引用“Microsoft.Office.Interop.Excel”对象。
谢谢 皮特
====找到答案==== 从您添加的其他类获取工作表的方法是简单地访问
Globals.ThisAddIn.Application.ActiveSheet;
,例如:
Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddin.Application.ActiveSheet;
其中“ThisAddIn”是您的向导创建的类的名称(您可能已重命名它)。
因此,请使用 Globals 来获取 ThisAddin 代码之外的 Excel 对象。
I have created an Excel Addin using the VSTO Template (VS2010, Excel2007).
In the Solution Explorer, I have a group called Excel, and under that a file called ExcelAddIn.cs. This has access to the active worksheet through code like
public partial class MyAddIn
{
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Application.Activesheet;
Excel.Range firstRow = activeWorksheet.get_Range("A1",missing);
}
etc. This code works fine ie. I can get at the Excel model.
Rather than put all my processing code in this one class file however, I'd like to work on Excel worksheet data in another class file. I have created this file, but am unable to use any code like the above in it, ie. I cannot seem to access the Excel model from this file.
I've duplicated the 'using Microsoft.Office.Tools.Excel' references, but putting in a line like:
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Application.Activesheet;
gives me a 'The name 'Application' does not exist in the current context' error.
Any ideas on what references/changes I need to make to get at the Excel model from this separate class file?
btw. The file that works refers to an 'Excel.Application' object for that first code line, the second separate file that doesn't work is referring to 'Microsoft.Office.Interop.Excel' object.
Thanks
Pete
==== FOUND ANSWER ====
The way to get at your worksheets from other classes you add is to simply access
Globals.ThisAddIn.Application.ActiveSheet;
for example:
Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddin.Application.ActiveSheet;
where 'ThisAddIn' is the name of the class your wizard created (you may have renamed it).
So, use Globals to get at your Excel objects outside the ThisAddin code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您自己的答案在这里:
从您添加的其他类获取工作表的方法是简单地访问,
例如:
其中“ThisAddIn”是您的向导创建的类的名称(您可能已重命名它)。
因此,请使用 Globals 来获取 ThisAddin 代码之外的 Excel 对象。
Your own answer here:
The way to get at your worksheets from other classes you add is to simply access
for example:
where 'ThisAddIn' is the name of the class your wizard created (you may have renamed it).
So, use Globals to get at your Excel objects outside the ThisAddin code.
可能是因为应用程序仅在 Excel 可执行文件运行时才存在?请记住,插件是专门为此设计的,并且可以在后台有一个“特殊酱汁”,使其可以与 Office 类交互。
我认为您无法从另一个类调用 Application.Activesheet ,因为您调用的类完全不知道应用程序的存在。
我意识到我可能有点胡言乱语,我希望其中一些是有道理的:)
简而言之,不,我不相信你可以这样调用 Application.Activesheet ,除非也许你传递了“Application”对象通过类实例化中的构造函数。
IE。
...
我不确定这是否有效,因为我目前没有启动我的虚拟机:)
祝你好运!
It could be because Application only ever exists when the Excel executable is running? Keep in mind Addins are specifically designed for this, and could have a 'special sauce' in the background that lets it interact the Office classes.
I dont think you would be able to call Application.Activesheet from another class as the Class you are calling from is completely unaware of the application's existence.
I realise I might be rambling a tad, I hope some of that made sense :)
In short, no I dont believe you can call Application.Activesheet like that, unless maybe you passed the 'Application' object through a constructor in your class instanciation.
ie.
...
Im not sure if that would work though as I dont currently have my VM booted up :)
Good luck!