1) open existing xlsx file with two worksheets ("Data" - contains sample data, "PivotChart" - with pivot table and pivot chart related to data from "Data" worksheet).
2) remove all data from "Data" worksheet and it with new data.
3) save chart image to image file.
Expected result - image file contains chart with new data inserted to "Data" worksheet (step 2).
Current result - image file contains chart with old data (from source xlsx file).
My question is: How force Spire.Xls to refresh PivotTable data and refresh chart related with the PivotTable?
I'm attaching sample xlsx file "PivotTable.xlsx"
I use following code:
- Code: Select all
foreach (Worksheet sheet in workbook.Worksheets)
{
foreach (XlsPivotTable pt in sheet.PivotTables)
{
pt.Cache.IsInvalidData = true;
pt.Cache.IsRefreshOnLoad = true;
pt.Cache.IsUpgradeOnRefresh = true;
}
}
workbook.CalculateAllValue();
Then
- Code: Select all
foreach (Worksheet sheet in workbook.Worksheets)
{
foreach (XlsPivotTable pt in sheet.PivotTables)
{
pt.CalculateData();
}
for (int i = 0; i != sheet.Charts.Count; ++i)
{
System.Drawing.Image chartImage = workbook.SaveChartAsImage(sheet, i);
chartImage.Save($"chart {sheet.Index}-{i}.png", System.Drawing.Imaging.ImageFormat.Png);
}
}