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)
{
var spreadsheetLocation = "C:\\update_rapport\\Salgsrapport.xlsx";
var exApp = new Microsoft.Office.Interop.Excel.Application();
var exWbk = exApp.Workbooks.Open(spreadsheetLocation);
exWbk.RefreshAll();
exApp.DisplayAlerts = false;
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.