Opening Excel ODBC connection from C #

I am looking to create a process that opens several excel files and just updates them. I have a code:

   excel.Visible = true;
    Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open("Testfile.xlsx");

    workbook.RefreshAll();
    workbook.Save();

The problem is that since on these sheets the connections are established for the update in the background, which it tries to save before it completes the update. I know that I need to set BackgroundQuery = false, but I do not know how to access existing connections. Any help is appreciated.

+3
source share
2 answers

So I was not able to get this specific code to work, but from the trial version and the error, I realized that:

foreach (Microsoft.Office.Interop.Excel.WorkbookConnection i in workbook.Connections)
        { System.Console.WriteLine(i.Name);
        i.OLEDBConnection.BackgroundQuery = false;
        }

work.

+1
source

C, , , :

Microsoft.Office.Interop.Excel.Worksheet ws = workbook.worksheets(1)
Microsoft.Office.Interop.Excel.Worksheet qt = ws.querytables(1)

ws.QueryTables(1).RefreshOnFileOpen = False
ws.QueryTables(1).BackgroundQuery = False
0

All Articles