Display and update all pivot tables

I am trying to create a script that updates all the data in a worksheet and then updates the pivot tables after that (since the data in the pivot tables is usually updated before the data from the Databases does not match the default result).

For this, I use this script (because I start this script automatically every day at 9.00).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            // Get fully qualified path for xlsx file
            var spreadsheetLocation = "C:\\update_rapport\\Salgsrapport.xlsx";

            var exApp = new Microsoft.Office.Interop.Excel.Application();
            var exWbk = exApp.Workbooks.Open(spreadsheetLocation);
            //var exWks = (Microsoft.Office.Interop.Excel.Worksheet)exWbk.Sheets["responses(7)"];

            exWbk.RefreshAll();

            exApp.DisplayAlerts = false;

            // This part is not correct. Need to find all pivot tables and update them
            Object PivotTables(
                Object Index
            );


            string save_file_name = "C:\\temp\\updated\\Salgsrapport.xlsx";
            exWbk.SaveAs(save_file_name);
            exWbk.Close(true);
            exApp.Quit();

        }

    }
}

The closest thing I discovered is iterating over all pivot tables: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.pivottables.aspx

However, this is not a complete example, and I never programmed C # before I got lost here. There may be a simpler solution to this problem, any hint will be appreciated.

+4
2

, ... , .

, PivotCaches , , Pivot Table. , - , - :

foreach (Microsoft.Office.Interop.Excel.PivotCache pt in exWbk.PivotCaches())
    pt.Refresh();

, , :

exWbk.Sheets["Sheet1"].PivotTables["PivotTable1"].PivotCache.Refresh();

.

, , , , exWbk.RefreshAll(); . , , , .

Update:

. , .

http://www.pcreview.co.uk/threads/vsto-how-to-find-all-pivot-tables-in-the-workbook.3476010/

Excel.PivotTables pivotTables1 =
   (Excel.PivotTables)ws.PivotTables(Type.Missing);

if (pivotTables1.Count > 0)
{
    for (int j = 1; j <= pivotTables1.Count; j++)
}
+4

foreach (Microsoft.Office.Interop.Excel.PivotCache pt exWbk.PivotCaches())

pt.Refresh();
0

All Articles