0

我们正在尝试从其中包含加载项公式的单元格中检索计算值。示例加载项“myUtilityl.xla”在 excel 中正常工作。它检索插件函数的值=ISOWEEKNUM(F9)。但是我们无法使用 C# 和 Microsoft 对象库以编程方式检索值。加载项“myUtilityl.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 对象库的新手。任何帮助或线索都会非常有帮助。

4

2 回答 2

1

Brijesh 现在正在工作。唯一缺少的是我们必须打开 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 计算值。

于 2012-01-06T09:06:15.480 回答
0

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

        var addins = Application.AddIns.Add(xlaFilePath);

        if (!addins.Installed)
        {
            addins.Installed = true;                  
        }
于 2012-01-04T09:42:10.423 回答