如何使用EPPlus创build数据透视表来过滤一列中的行以及一组列?

要创build数据透视表,需要提供源数据以从中生成数据透视表。

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

在这里输入图像描述

我需要从这些数据生成一个数据透视表,这样它就像这样生成:

在这里输入图像描述

IOW,它需要在“描述”列上有一个filter,允许行被过滤(只显示“辣椒”或其他),并filter显示月份列(该表可以有多达13个月的列9月15日,10月15日等)),以便用户可以select那些“月份年份”列中的1..13列来显示(技术上,他们可以select0,但是重点是什么)?

如何才能做到这一点? 我已经尝试了以下内容:

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 } 

…但只获得“说明”的筛选器,其下方没有数据:

在这里输入图像说明

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

尝试这个:

像往常一样创build整个PivotTable ,并添加所有必填字段和计算后,将DataPivotFields作为RowFields移动。

在VBA中会是这样的:

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

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

PD。 用您的代码中的数据透视表对象replace数据透视表