3

我有一个 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 中的所有数据。

4

3 回答 3

1

完成刷新后尝试将代码更改为

excelWorkbook.SaveCopyAS("Your File save location with FileName");

代替

excelWorkbook.Save();

可能对您的情况有所帮助。

于 2013-06-05T14:15:42.507 回答
1

我能够在 http://metacpan.org/pod/Win32::Excel::Refresh使用 XLRefresh.exe 进行刷新

于 2013-06-27T16:01:44.177 回答
0

这是刷新 Excel 并运行连接到 SQL Server/MS Access 以填充数据的后台查询的代码

excelWorkbook.RefreshAll();
excelApp.Application.CalculateUntilAsyncQueriesDone(); // This condition will wait till background query execution is completed.

请让我知道这可不可以帮你。

于 2017-12-18T13:13:43.887 回答