gpt4 book ai didi

c# - 在 C# 中刷新 Excel 工作表

转载 作者:行者123 更新时间:2023-12-04 19:49:30 25 4
gpt4 key购买 nike

我有一个 excel 文件,它在“工作表 1”上有数据透视表和图表,引用了“工作表 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 中的所有数据。

最佳答案

刷新后尝试将代码更改为

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

代替

excelWorkbook.Save();

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

关于c# - 在 C# 中刷新 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com