Open Excel File, Refresh Query and Save C#

2019-08-06 21:45发布

I am trying to open an Excel file, refresh the query behind it and then save and close the file in C#.

I have the following but I am getting errors on the following line first up:

        object _missingValue = System.Reflection.Missing.Value;
        Application excel = new Application();
        Workbook theWorkbook = excelFile.Workbooks._Open(txtLocation.Text, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue);
        Sheets sheets = (Sheets)theWorkbook.Worksheets;
        theWorkbook.RefreshAll();
        theWorkbook.Save();
        excel.Quit();

I have added the Microsoft.Office.Interop.Excel reference, but still have the issue, what I am missing?

标签: c# excel-2010
1条回答
走好不送
2楼-- · 2019-08-06 22:00

Resolved with the following:

object _missingValue = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Workbook theWorkbook = excel.Workbooks.Open(txtLocation.Text, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue);
Sheets sheets = (Sheets)theWorkbook.Worksheets;
theWorkbook.Save();
excel.Quit();
return true;

Thanks!

查看更多
登录 后发表回答