刷新Excel文件,从SQL Server获取其数据与C#

我一直在寻找c#的教程来刷新一个excel文件,而无需打开excel并单击刷新button。 我find了解决scheme,这很容易。 我想分享

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"); } 

为什么不告诉excel文件打开时刷新? 数据 – >连接,然后选中“打开文件时刷新数据”

更简单的解决scheme。