使用 Spire.Xls 包通过 C# 代码自动将过滤器添加到 Excel 中数据透视表中的每个列标题。
PivotField r1 = pt.PivotFields["Invoice Type"] as PivotField;
r1.Axis = AxisTypes.Row;
r1.RepeatItemLabels = true;
r1.Subtotals = SubtotalTypes.None;
pt.Options.RowHeaderCaption = "Invoice Type";
在这里,我创建了一个数据透视字段,该字段添加到数据透视表中的行中,并作为列标题显示在数据透视表中,我需要相同的过滤器
PivotField r1 = pt.PivotFields["Invoice Type"] as PivotField;
r1.Axis = AxisTypes.Row;
r1.RepeatItemLabels = true;
r1.Subtotals = SubtotalTypes.None;
pt.Options.RowHeaderCaption = "Invoice Type";
foreach (var field in pt.RowFields)
{
PivotReportFilter filter = new PivotReportFilter(field.Name, true);
pt.ReportFilters.Add(filter);
}
field.Name - 没有 Name 的定义
将 foreach(pt.RowFields 中的 var 字段)更改为 foreach(pt.RowFields 中的PivotField 字段)。