Wait for Workbook.RefreshAll() (C#)

前端 未结 4 732
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 15:50

I want to loop through a directory (using C#) and Refresh all Excel sheets in there. I use:

Workbook.RefreshAll();

How can I wait for the <

相关标签:
4条回答
  • 2021-01-18 16:14

    If you go through your Workbook and set the the BackgroundQuery property of all of your QueryTables to false, RefreshAll should finish before moving on.

    foreach (Worksheet worksheet in workbook.Worksheets)
    {
        foreach (QueryTable table in worksheet.QueryTables)
            table.BackgroundQuery = false;
    }
    
    //This will finish executing before moving on
    workbook.RefreshAll();
    
    0 讨论(0)
  • 2021-01-18 16:21

    Maybe you can query for the State:

    Application.CalculationState =xldone

    For Documentation look here: http://msdn.microsoft.com/en-us/library/office/bb220901(v=office.12).aspx

    0 讨论(0)
  • 2021-01-18 16:28

    Updated 31.05.2016...

    Thanks for your help. I found the following solution:

    foreach (MSExcel.WorkbookConnection cnn in wb.Connections)
    {
        if (cnn.Type.ToString() == "xlConnectionTypeODBC")
        {
            cnn.ODBCConnection.BackgroundQuery = false;
        }
        else
        {
            cnn.OLEDBConnection.BackgroundQuery = false;
        }
    }
    

    Frank

    0 讨论(0)
  • 2021-01-18 16:32

    The below code will work perfectly when your excel has to run background query to pull data from SQL Server/MS Access and it will wait for the query is successful.

    public void RefreshSheet(Excel.Application Excel, Excel.Workbook WB)
    {
        WB.RefreshAll();
        Excel.Application.CalculateUntilAsyncQueriesDone();
        WB.save();
    }
    

    Here WB is the workbook and Excel is the Excel Application declared in your code.

    0 讨论(0)
提交回复
热议问题