我正在尝试刷新excel电子表格中的所有数据。我创建了一个C#脚本,如下所示:
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
Workbook excelWorkbook = null;
try
{
excelWorkbook = excelApp.Workbooks.Open(workbookPath);
}
catch (Exception ex)
{
Console.Writeline("Errore nella fase di apertura del file: " + ex.Message);
}
try
{
excelWorkbook.RefreshAll();
excelApp.Application.CalculateUntilAsyncQueriesDone();
excelWorkbook.SaveAs(filename);
}
catch (Exception ex)
{
Console.Writeline("Errore nella fase di aggiornamento del file e salvataggio");
}
finally
{
excelWorkbook.Close(false, workbookPath, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
excelWorkbook = null;
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
}
我的C#脚本可以正常运行,但与计划任务一起使用时不会刷新数据。 C#脚本的其余部分都可以正常工作。任何帮助将不胜感激。
注意:为了避免错误,我必须在服务器中创建两个文件夹(C:\ Windows \ SysWOW64 \ config \ systemprofile \ desktop和C:\ Windows \ System32 \ config \ systemprofile \ desktop)
答案 0 :(得分:0)
关闭工作簿时,您没有保存所做的更改,因此,即使“独自运行”,我也不知道该如何工作。尝试更改行:
excelWorkbook.Close(false, workbookPath, null);
收件人:
excelWorkbook.Close(true, workbookPath, null);