【发布时间】:2021-07-23 13:20:33
【问题描述】:
我正在编写一个程序,该程序从资源文件夹加载一个 excel,运行一个 makkro 并将 makkro 的输出保存到一个变量中。但是每次我尝试运行我的 makkro 时,程序都会告诉我它找不到我的 excel 所在的文件。现在我想知道我的程序是否能够到达临时文件路径,或者是否发生了与此不同的任何事情。
我认为这里的问题可能是复制 excel 会停用 makkros,我应该在复制后再次激活它们。如果这是一个现实问题,有人可以向我解释我如何才能超越这个问题吗?
编辑
代码似乎也能够读取临时文件。至少它找到了excel表。现在唯一仍然存在的问题是 makkros。
public void CreateCopyOfResourcesExcel()
{
//temporary filepath to read the excel
tempPath = System.IO.Path.GetTempFileName();
//copy excel to this filepath
System.IO.File.WriteAllBytes(tempPath, Properties.Resources.Vorlage_Sensoren);
excelApp = new _Excel.Application();
}
private void button_WFx_Click(object sender, EventArgs e)
{
excelWorkbook = excelApp.Workbooks.Open(tempPath);
excelSheet = excelWorkbook.Sheets["Tabelle2"];
excelApp.Visible = false;
//run measurement
excelApp.Run("Vorlage_Sensoren.xlsm!readValue"); //<-- here comes the error (System.Runtime.InteropServices.COMException)
RunSensorExcel(); //this is the function where I wanted to save the values
//close excel
excelWorkbook.Close(false);
excelApp.Quit();
Marshal.ReleaseComObject(excelWorkbook);
Marshal.ReleaseComObject(excelApp);
}
【问题讨论】:
标签: c# excel excel-interop temporary-files comexception