简体   繁体   中英

C# Assembly with 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM