使用 ExcelDNA 进行 C# 汇编

发布于 2024-11-15 13:00:27 字数 976 浏览 2 评论 0原文

我正在将 VSTO 应用程序转换为与 ExcelDNA 兼容的应用程序。豪维 主要问题是 ExcelDNA 与 VSTO 相比没有控件对象。

在 VSTO 中: Microsoft.Office.Tools.Excel:您可以添加一个 listObject

Worksheet worksheet = Globals.Factory.GetVstoObject(
    Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);

listObj = worksheet.Controls.AddListObject(cell, "list1");

随后,您可以设置数据源

listObj.DataSource=list;

但是,当我尝试使用 Micosoft.Office.Interop.Excel 在 ExcelDNA 中使用 listObject 进行操作时。我无法获得所需的结果,listObject 返回空白数据。

ws=excelApp.ActiveWorkBook.ActiveSheet;
Excel.Range rng=ws.cells[1,1];
//set the datasource
rng.Value2=list;
listObj=this.ListObjects.Add(
    Excel.XlListObjectSourceType.xlSrcRange, rng,Missing.Value,
    Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, Missing.Value);

我无法使用 Globals.Factory...因为这不是 VSTO 程序。因此,我想出了以下解决方法。我做错了什么吗?我怀疑是 ExcelDNA 中的数据源造成了问题。

我能做什么来解决这个问题?我该如何将 VSTO 程序转换为 ExcelDNA 中的等效程序?

I am converting an VSTO app to one that is compatible with ExcelDNA. Howeve
r the main problem is that ExcelDNA does not have the controls object as compared to VSTO.

In VSTO: Microsoft.Office.Tools.Excel: you can add a listObject

Worksheet worksheet = Globals.Factory.GetVstoObject(
    Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);

listObj = worksheet.Controls.AddListObject(cell, "list1");

Subsequently, you can set the datasource

listObj.DataSource=list;

However, when I tried to do it in ExcelDNA using Micosoft.Office.Interop.Excel using the listObject. I cannot get the desired result, the listObject returned blank data.

ws=excelApp.ActiveWorkBook.ActiveSheet;
Excel.Range rng=ws.cells[1,1];
//set the datasource
rng.Value2=list;
listObj=this.ListObjects.Add(
    Excel.XlListObjectSourceType.xlSrcRange, rng,Missing.Value,
    Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, Missing.Value);

I can't use the Globals.Factory... as this is not a VSTO program. Thus, I came up with the following workaround. Is there anything I am doing wrongly? I suspect it's the datasource that is giving the problem in ExcelDNA.

What can I do to solve that? How am I suppose to convert the VSTO program to the equivalent in ExcelDNA?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋凉 2024-11-22 13:00:27

事实上,从 Excel-DNA 加载项中,您只能访问 Excel COM 对象模型,而不能访问 VSTO 扩展。但是,VSTO 程序集只是通过 COM 接口与 Excel 通信,因此理论上您应该能够从 Excel-DNA 加载项执行相同的操作,也许在添加一些帮助程序类之后。

我建议您可以尝试弄清楚如何从 VBA 创建列表对象,也许可以通过记录和检查一些执行您想要执行的操作的宏。如果您能够通过 VBA 按您想要的方式操作 Excel,那么您当然也可以通过 Excel-DNA 加载项执行相同操作。

一个可能的问题是,我认为某些对象模型仅通过 COM 调度接口公开。此类方法通过互操作程序集不可见,并且可能必须通过反射、从 VB.NET 或使用 C# 4 中的“动态”支持来调用。一旦您能够从 VBA 进行正确的调用,我很高兴帮助了解如何从 Excel-DNA 加载项执行此操作。

Indeed from your Excel-DNA add-in you only have access to the Excel COM object model and not the VSTO extensions. However, the VSTO assemblies just talk to Excel through the COM interface, so in theory you should be able to do the same from your Excel-DNA add-in, perhaps after adding some helper classes.

I suggest you could try to figure out how to create the list object from VBA, perhaps by recording and inspecting some macros that do what you would like to. If you are able to manipulate Excel the way you want from VBA, you can certainly do the same from your Excel-DNA add-in.

A possible issue is that I believe some of the object model is exposed only through the COM dispatch interface. Such methods won't be visible through the interop assembly and might have to called via reflection, from VB.NET or with the "dynamic" support in C# 4. Once you are able to make the right calls from VBA, I'm happy to help figure out how to do it from your Excel-DNA add-in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文