我们正在尝试从包含插件公式的单元格中检索计算值。
示例加载项“myUtilityl.xla”在excel中正常运行。它检索addin函数=ISOWEEKNUM(F9)
的值。但我们无法使用C#& amp;编程检索值。 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();
我是微软对象库的新手。任何帮助或线索都会非常有用。
答案 0 :(得分: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计算值。
答案 1 :(得分:0)
您可以在代码示例中添加以下内容
var addins = Application.AddIns.Add(xlaFilePath);
if (!addins.Installed)
{
addins.Installed = true;
}