【问题标题】:refreshing excel file which gets its data from sql server with c#使用c#刷新从sql server获取数据的excel文件
【发布时间】:2013-07-05 09:10:38
【问题描述】:

我一直在搜索 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");
    }

【问题讨论】:

    标签: c# sql-server excel excel-interop


    【解决方案1】:

    为什么不直接告诉 excel 在打开文件时进行刷新? 数据->连接,然后勾选“打开文件时刷新数据”

    更简单的解决方案。

    【讨论】:

      猜你喜欢
      • 2017-05-05
      • 1970-01-01
      • 2012-07-14
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多