Open Excel File, Refresh Query and Save C#

2019-08-06 21:56发布

问题:

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?

回答1:

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!



标签: c# excel-2010