简体   繁体   English

使用C#Excel Interop在Excel中计算用户定义的函数

[英]Calculating User Defined Functions in Excel using C# Excel Interop

I am trying to an Add-in directly from C# so that when I open a workbook, and do a Workbook.Calculate() the UDF's (User Defined Functions) that are defined in an external addin correctly calculate in the worksheet. 我正在尝试直接从C#中创建外接程序,以便在我打开工作簿并执行Workbook.Calculate()时,可以在工作表中正确计算在外部外接程序中定义的UDF(用户定义函数)。 Currently, I am looping through each adding and simple setting: 目前,我正在遍历每个添加和简单的设置:

AddIn.Installed = true

This does not work. 这是行不通的。 C# does not load add-in at all, and I want to avoid using VBA. C#根本不加载加载项,我想避免使用VBA。 I want to open a workbook an excel workbook with the specific add in loaded, do a full calculated, and should have all values of the worksheet updated, including cells with UDF's. 我想打开一个工作簿并加载一个特定的excel工作簿,进行完整的计算,并且应该更新工作表的所有值,包括带有UDF的单元格。

Thanks for any help.... 谢谢你的帮助....

Some code: 一些代码:

Excel.Workbook wkbk = ExcelApp.ActiveWorkbook;

        Excel.XlFixedFormatType paramExportFormat = Excel.XlFixedFormatType.xlTypePDF;
        Excel.XlFixedFormatQuality paramExportQuality = Excel.XlFixedFormatQuality.xlQualityStandard;
        bool paramOpenAfterPublish = false;
        bool paramIncludeDocProps = true;
        bool paramIgnorePrintAreas = true;
        object paramFromPage = Type.Missing;
        object paramToPage = Type.Missing;

        ExcelApp.Visible = true;
        //foreach (Excel.AddIn aiTemp in ExcelApp.AddIns)
        //{
        //    if (aiTemp.Name.Contains(""))
        //    {
        //        aiTemp.Installed = false;
        //        aiTemp.Installed = true;
        //    }
        //}            

        while (ExcelApp.CalculationState == Microsoft.Office.Interop.Excel.XlCalculationState.xlCalculating)
        {
            Thread.Sleep(50);
        }
        ExcelApp.CalculateFull();
        var wksht = wkbk.ActiveSheet;
        Excel.Range rng = ((Excel.Worksheet)wksht).get_Range("B1", "B1");
        rng.Calculate();
        //EnsureCalcFinished();

        ExcelApp.Visible = false;
        wkbk.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, PathToDocument.Replace(".xlsx", ".pdf"), paramExportQuality, true, false, Type.Missing, Type.Missing, true,Type.Missing);

UPDATE: I found a link with the method I use to register UDFs. 更新:我找到了用于注册UDF的方法的链接。

Ref: http://msdn.microsoft.com/en-us/library/ms173189(v=vs.80).aspx 参考: http : //msdn.microsoft.com/zh-cn/library/ms173189( v= vs.80).aspx

In Excel, you need to go to Options -> Add-Ins => Excel Add-in (Go..) => automation = select the library and hit OK. 在Excel中,您需要转到选项->加载项=> Excel加载项(Go ..)=>自动化=选择库,然后单击确定。 Once you do this once, it'll be auto-loaded each time you open excel. 一旦执行一次,每次打开excel时都会自动加载。

Here is the algorithm to load excel AddIns so that when you open excel workbook, a calculation will include UDF's defined in Add-in: 这是加载excel加载项的算法,因此当您打开excel工作簿时,计算将包括在加载项中定义的UDF:

1)Initiliaze Excel Application 1)初始化Excel应用程序

ExcelApp = new Excel.Application();

2)Load AddIns * 2)加载插件*

 foreach (Excel.AddIn ai in ExcelApp.AddIns)
        {
            ai.Installed = false;
            ai.Installed = true;
            ExcelApp.Wait(50);
        }

**The Key is to load add-ins before you open Excel Workbook. **关键是在打开Excel工作簿之前加载加载项。

3)Open Excel Workbook, which will trigger calculations 3)打开Excel工作簿,这将触发计算

4)Set Calculation Mode to manual so that any changes in Interop do not trigger lengthy recalc 4)将“计算模式”设置为“手动”,以便Interop中的任何更改都不会触发冗长的重新计算

ExcelApp.Calculation = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationManual;  

5)Perform any manipulations, and perform calc 5)执行任何操作并执行计算

ExcelApp.CalculateFull();

6)Dispose of Excel Objects appropriately 6)适当处置Excel对象

Hope this helps someone with a similar issue.. Ended up being a simple fix for a simple problem. 希望这对遇到类似问题的人有所帮助。.最终是解决一个简单问题的简单方法。 Just remember to load add-ins before opening the workbook. 只记得在打开工作簿之前加载加载项。 Otherwise, opening an excel workbook with UDF's dependent on AddIn will fail. 否则,使用依赖于AddIn的UDF打开Excel工作簿将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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