Hello!
Our application dynamically generates Excel files, and can include pivot tables. While in some cases it works, in most cases the Excel file generated is corrupted and at least one Pivot table is removed. I am using the latest version of Aspose cells, 18.5.
Below is the method that the console application references:
protected void AddPivotTables(Worksheet dataSheet)
{
GeneratedComponent.CalculateFormula();
Worksheet pivotSheet = GeneratedComponent.Worksheets[GeneratedComponent.Worksheets.AddCopy(dataSheet.Name)];
int lastDataRow = pivotSheet.Cells.GetLastDataRow(tableStart.Column);
pivotSheet.Cells.DeleteRows(tableStart.Row, (lastDataRow - tableStart.Row) + 1);
string pivotSheetName = dataSheet.Name + " - Pivot";
pivotSheet.Name = pivotSheetName.Length > 31 ? pivotSheetName.Substring(0, 31) : pivotSheetName;
//since we may have had a RowFormatting Column, or other tables may have been wider than the current table, get the last row's last data cell
Cell lastRowLastDataCell = dataSheet.Cells.Rows[dataSheet.Cells.LastCell.Row].LastDataCell ?? dataSheet.Cells.LastCell;
PivotTable pivotTable = pivotSheet.PivotTables[pivotSheet.PivotTables.Add(string.Format("={0}!{1}:{2}", dataSheet.Name, tableImportStart.Name, lastRowLastDataCell.Name), tableStart.Name, dataSheet.Name.Replace(" ", "") + "PivotTable")];
pivotTable.RowGrand = true;
pivotTable.IsAutoFormat = true;
pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight6;
pivotTable.ShowPivotStyleColumnHeader = true;
pivotTable.ShowPivotStyleRowStripes = true;
pivotTable.ShowPivotStyleColumnStripes = true;
for (int i = 0; i < pivotTable.BaseFields.Count; i++)
pivotTable.AddFieldToArea(PivotFieldType.Row, i);
pivotSheet.MoveTo(GeneratedComponent.Worksheets.IndexOf(dataSheet));
}
And here is the code I ran through the VS console:
GeneratedComponent = new Workbook("C:\\temp\\InputFile.xlsx");
tableImportStart = GeneratedComponent.Worksheets[0].Cells["A15"];
tableStart = tableImportStart;
AddPivotTables(GeneratedComponent.Worksheets[0]);
GeneratedComponent.Save("C:\\temp\\OutputFile.xlsx");
Attached are three files:
OriginalSave.xlsx - The files that our application Generates
InputFile.xlsx - The test file for the input
OutputFile.xlsx - The outputted file from the code run here.
AsposeFiles.zip (81.4 KB)
I can also provide more test files upon request.