【问题标题】:How can I create a PivotTable using EPPlus to filter on the rows in one column and also a set of columns?如何使用 EPPlus 创建数据透视表以过滤一列中的行以及一组列?
【发布时间】:2017-02-20 10:37:51
【问题描述】:

要创建数据透视表,需要提供用于生成数据透视表的源数据。

我为此目的生成了一个“虚拟”表,其中包含以下原始数据:

我需要从该数据生成一个数据透视表,以便像这样生成它:

IOW,它需要在“描述”列上有一个过滤器,允许过滤行(仅显示“Peppers”或其他)和一个过滤器,用于显示哪些月份列(工作表最多可以有 13月列(9 月 15 日、10 月 15 日等))以便用户可以选择 1..13 个“月年”列来显示(从技术上讲,他们可以选择 0,但有什么意义)?

如何做到这一点?我尝试了以下方法:

private void AddPivotTable()
{
    var dataRange 
rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Address];
    dataRange.AutoFitColumns();
    var pivotTable 
usagePivotWorksheet.PivotTables.Add(usagePivotWorksheet.Cells["A6"]
dataRange, "ProdUsagePivot");
    pivotTable.MultipleFieldFilters = true;
    pivotTable.GridDropZones = false;
    pivotTable.Outline = false;
    pivotTable.OutlineData = false;
    pivotTable.ShowError = true;
    pivotTable.ErrorCaption = "[error]";
    pivotTable.ShowHeaders = true;
    pivotTable.UseAutoFormatting = true;
    pivotTable.ApplyWidthHeightFormats = true;
    pivotTable.ShowDrill = true;

    var descPageField = pivotTable.Fields["Description"];
    pivotTable.PageFields.Add(descPageField);
    descPageField.Sort
OfficeOpenXml.Table.PivotTable.eSortType.Ascending;

    var descRowField = pivotTable.Fields["Description"];
    pivotTable.RowFields.Add(descRowField);

    . . . add others later
} 

...但只获取“描述”的过滤器,其下方没有数据:

我还需要做什么才能获得所需的外观/功能?

【问题讨论】:

    标签: excel pivot-table epplus epplus-4


    【解决方案1】:

    试试这个:

    照常创建整个PivotTable,并在添加所有必填字段和计算后,将DataPivotFields 移动为RowFields

    在 VBA 中会是这样的:

    With PivotTable
        .DataPivotField.Orientation = xlRowField
        .Position = 2
    End With
    

    还需要应用此:PivotTable.MergeLabels = True 以合并所有相应总计的 Description 单元格。

    警察局。用代码中的数据透视表对象替换数据透视表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 2019-11-10
      • 2020-11-13
      • 1970-01-01
      • 2018-03-26
      • 2023-04-03
      • 2017-07-06
      相关资源
      最近更新 更多