我的任务是用数据填充 Excel 模板(在工作表 1 上)。该工作表中的数据将用于更新其他工作表上的 5 或 6 个数据透视表。到目前为止,下面的代码已经完成了这些工作。但是,我正在尝试找出如何从最终的数据透视表结果中删除 0 美元金额。
public void PivotTableTesting(int recCount)
{
PivotTableCacheDefinitionPart ptp = book.PivotTableCacheDefinitionParts.FirstOrDefault();
PivotTableDefinition def = sheetPart.PivotTableParts.FirstOrDefault().PivotTableDefinition;
ptp.PivotCacheDefinition.RefreshOnLoad = true;
ptp.PivotCacheDefinition.RecordCount = UInt32Value.ToUInt32((uint)recCount);
string val = ptp.PivotCacheDefinition.CacheSource.WorksheetSource.Reference.Value;
ptp.PivotCacheDefinition.CacheSource.WorksheetSource.Reference = @"A1:AO" + (recCount + 1).ToString();
ptp.PivotTableCacheRecordsPart.PivotCacheRecords.RemoveAllChildren();
ptp.PivotTableCacheRecordsPart.PivotCacheRecords.Count = 0;
}
有人有关于创建过滤器并将其应用于数据透视表定义的示例代码吗?
在这里面对同样的问题,我需要创建两个Excel,两者几乎相同,除了一个Excel带有数据透视表过滤器,另一个Excel没有
我使用 Open XML SDK 2.5 Productivity Tool 对代码进行逆向工程,如何安装请阅读此处
因为源代码太长,我只列出了下面的差异
对于带有过滤器的代码
// Generates content of pivotTablePart1.
private void GeneratePivotTablePart1Content(PivotTablePart pivotTablePart1)
...
Location location1 = new Location(){ Reference = "A5:B9", FirstHeaderRow = (UInt32Value)1U, FirstDataRow = (UInt32Value)1U, FirstDataColumn = (UInt32Value)1U, RowPageCount = (UInt32Value)1U, ColumnsPerPage = (UInt32Value)1U };
...
PivotField pivotField10 = new PivotField(){ Axis = PivotTableAxisValues.AxisPage, ShowAll = false };
...
PageFields pageFields1 = new PageFields(){ Count = (UInt32Value)1U };
PageField pageField1 = new PageField(){ Field = 9, Hierarchy = -1 };
pageFields1.Append(pageField1);
...
pivotTableDefinition1.Append(pageFields1);
对于没有过滤器的代码
// Generates content of pivotTablePart1.
private void GeneratePivotTablePart1Content(PivotTablePart pivotTablePart1)
...
Location location1 = new Location(){ Reference = "A5:B9", FirstHeaderRow = (UInt32Value)1U, FirstDataRow = (UInt32Value)1U, FirstDataColumn = (UInt32Value)1U };
...
PivotField pivotField10 = new PivotField(){ ShowAll = false };
...
// removed PageFields pageFields1
...
// removed pivotTableDefinition1.Append(pageFields1);
我将数据透视表放在“sheet2”上,对于字段= 9,它引用“sheet1”处的列J(从零开始的索引) 它是在GeneratePivotTableCacheDefinitionPart1Content()
下定义的 WorksheetSource worksheetSource1 = new WorksheetSource(){ Reference = "A1:K7", Sheet = "Sheet1" };