Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
Workbook theWorkbook = excelApp.Workbooks._Open(XlsxPath, 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false);
Sheets sheets = (Sheets)theWorkbook.Worksheets;
theWorkbook.RefreshAll();
System.Threading.Thread.Sleep(Convert.ToInt32(60000));
theWorkbook.Save();
excelApp.Quit();

is working fine but the flowing is not working
C#
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
Workbook theWorkbook = excelApp.Workbooks._Open(XlsxPath, 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false);
Sheets sheets = (Sheets)theWorkbook.Worksheets;
theWorkbook.RefreshAll();
theWorkbook.Save();
excelApp.Quit();


Why please help me i don't want to include
C#
System.Threading.Thread.Sleep(Convert.ToInt32(60000));


because i don't know how much time it will take for refreshing

Please help me


Thanks
Sudheer.N
Posted
Updated 21-Oct-13 23:55pm
v2

1 solution

Hi Sudheer,

RefreshAll behaviour depends on the BackgroundQuery property of underlying objects.

If BackgroundQuery is set to true then RefreshAll returns the result immediately.

So use the following code to refresh data not in the background:

C#
foreach (Worksheet ws in theWorkbook.Worksheets)
    foreach (ListObject lo in ws.ListObjects)
    {
        QueryTable qt = lo.QueryTable;
        if (qt != null)
        {
            qt.Refresh(false);
        }
    }

C#
foreach (PivotCache pc in theWorkbook.PivotCaches())
{
    pc.Refresh();
}
 
Share this answer
 
v2
Comments
Sudheer Nimmagadda 22-Oct-13 6:59am    
Thank you very much Sergey Vaselenko, it is useful
Sudheer Nimmagadda 22-Oct-13 8:25am    
Sir ,

i have to refresh PivotTables in excel
your code is working excellent for tables but not PivotTable

please help me for PivotTable refreshing
Sergey Vaselenko 22-Oct-13 11:30am    
foreach (PivotCache pc in theWorkbook.PivotCaches())
{
pc.Refresh();
}
Sergey Vaselenko 23-Oct-13 14:52pm    
Have you solve your task with PivotTables, Sudheer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900