我一直在搜索 c# 的教程来刷新 excel 文件,而无需打开 excel 并单击刷新按钮。我找到了解决方案,这很容易。我想分享
private void ExcelRefresh(string Filename)
{
try
{
object NullValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
//excelApp.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook Workbook = excelApp.Workbooks.Open(
Filename, NullValue, NullValue, NullValue, NullValue,
NullValue, NullValue, NullValue, NullValue, NullValue,
NullValue, NullValue, NullValue, NullValue, NullValue);
Workbook.RefreshAll();
System.Threading.Thread.Sleep(20000);
Workbook.Save();
Workbook.Close(false, Filename, null);
excelApp.Quit();
Workbook = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
ExcelRefresh(@"D:\test.xlsx");
}