使用“AddIn Formulas”和“Microsoft Object Library”从Excel中读取计算值

本文关键字:Excel 读取 计算 Object Formulas AddIn Microsoft 使用 Library | 更新日期: 2023-09-27 18:19:15

我们正试图从其中有外接程序公式的单元格中检索计算值。示例外接程序"myutility"。Xla"在excel中工作正常。它为插件函数=ISOWEEKNUM(F9)检索值。但是我们无法用c# &微软对象库。加载项"myutility"。xla"附在Excel中。环境是VS2010

我在这里提供示例代码。

        string path = @"C:'Test.xls";
        Workbook theWorkbook;
        Worksheet theWorksheet;
        Range readRange;
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();            
        theWorkbook = app.Workbooks.Open(path);
        Sheets theSheets = (Sheets)theWorkbook.Worksheets;
        theWorksheet =  (Worksheet)theWorkbook.Worksheets.get_Item("Sheet1");            
        readRange = theWorksheet.get_Range("B1");            
        MessageBox.Show(Convert.ToString(readRange.Value));
        //theWorkbook.Save();
        app.Workbooks.Close();

我是Microsoft对象库的新手。

使用“AddIn Formulas”和“Microsoft Object Library”从Excel中读取计算值

好了,英国现在开始工作了。唯一缺少的是我们必须打开xla。app.Workbooks.Open (xlaFilePath);然后它开始工作了……非常感谢。我在这里张贴的代码无论如何

        string path = @"C:'Test2.xls";
        string xlaPath = @"C:'Test2.xla";
        Workbook theWorkbook;
        Worksheet theWorksheet, theWorksheet2;
        Range readRange;
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
        app.Workbooks.Open(xlaPath);
        theWorkbook = app.Workbooks.Open(path);
        theWorksheet2 = (Worksheet)theWorkbook.Worksheets.get_Item("Sheet2");
        theWorksheet2.get_Range("A3").Value = 7;
        theWorksheet2.get_Range("A4").Value = 7;
        theWorkbook.RefreshAll();
        theWorksheet = (Worksheet)theWorkbook.Worksheets.get_Item("Sheet1");           
        readRange = theWorksheet.get_Range("A1");
        Console.WriteLine(Convert.ToString(readRange.Value));
        Console.ReadLine();            //theWorkbook.Save();             
        theWorkbook.Close();
        app.Workbooks.Close();

上面的代码将两个值输入到sheet2的单元格中,并检索VBA UDF计算值。

您可以在代码示例中添加以下内容

        var addins = Application.AddIns.Add(xlaFilePath);
        if (!addins.Installed)
        {
            addins.Installed = true;                  
        }