ClosedXML 数据透视表 Table SetCalculation

ClosedXML Pivot Table SetCalculation

我正在尝试使用 ClosedXML 导出带有数据透视表的数据 table。我可以使用以下代码创建枢轴 table,但 SetCalculation 给我带来了一些麻烦。我想计算每年 Amount 列之间的差额。 Excel 工作表无法加载并导致整个 Excel.

崩溃
var pt = worksheet.PivotTables.AddNew("PivotTable", worksheet.Cell(rowIndex+3, 1), table.AsRange());

pt.ReportFilters.Add("Department");
pt.ReportFilters.Add("Month");
pt.ColumnLabels.Add("Year").SetCollapsed();
pt.RowLabels.Add("Account").SetCollapsed();
pt.RowLabels.Add("Description").SetCollapsed();
pt.Values.Add("Amount", "Amount").NumberFormat.NumberFormatId = 4;
pt.Values.Add("Diff","Diff").SetBaseField("Year").
SetBaseItem("Amount").SetCalculation
(XLPivotCalculation.DifferenceFrom).NumberFormat.NumberFormatId = 4;

我想通了:

pt.Values.Add("Amount","Diff%").SetCalculation(XLPivotCalculation.PercentageDifferenceFrom).SetBaseField("Year").SetCalculationItem(XLPivotCalculationItem.Previous) .NumberFormat.NumberFormatId = 10;