如何使用 C# 中的 Spire.Xls 包向数据透视表中的列标题添加过滤器

问题描述 投票:0回答:1

使用 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 的定义

c# pivot pivot-table spire.xls
1个回答
0
投票

将 foreach(pt.RowFields 中的 var 字段)更改为 foreach(pt.RowFields 中的PivotField 字段)。

© www.soinside.com 2019 - 2024. All rights reserved.