我有一个 excel 文件,它在“Sheet 1”上有数据透视表和图表,这些数据来自“Sheet 2”,这些数据又指向 SQL Server 表中的记录。
我编写了一个 SSIS 作业来填充底层 SQL Server 表,然后使用以下代码刷新 excel 表。
//At this point, sql server table is already populated with data.
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
try
{
excelWorkbook.RefreshAll();
excelWorkbook.RefreshAll();
excelWorkbook.RefreshAll();
excelWorkbook.Save();
}
finally
{
excelWorkbook.Close(false, workbookPath, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
excelWorkbook = null;
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
问题是当我打开excel时,它仍然显示以前加载的数据。在我单击 excel 文件中的“全部刷新”后,数据会被刷新。是否有任何万无一失的方法可以使用 C# 刷新 excel 中的所有数据。