我希望这至少可以帮助我回答我提出的两个问题之一 here ,但我正在寻找如何在 EPPlus/OpenXML 中创建 PivotCache,但在网上/他们的文档中找不到任何说明如何执行此操作的内容。
所以,假设我有一个 Excel 工作表,wksRawData
在 EPPlus 中创建,我想创建第二个工作表,其中的数据透视表基于 wksRawData.Cells(wksRawData. Dimension.Address)
- 希望我可以删除 wksRawData 但仍保留数据透视表。我该怎么做?
到目前为止,我在第二个工作表中创建数据透视表的代码是:
Dim wksRawData As ExcelWorksheet = wbk.Worksheets("Raw Data")
Dim wksPvtTbl As ExcelWorksheet = wbk.Worksheets("PivotTbl")
' Insert the Pivot Table to the sheet
Dim DataRange As ExcelRange = wksRawData.Cells(wksRawData.Dimension.Address)
Dim pvtTable As OfficeOpenXml.Table.PivotTable.ExcelPivotTable = wksPvtTbl.PivotTables.Add(wksPvtTbl.Cells("B4"), DataRange, "MyPivotTable")
pvtTable.Compact = True
pvtTable.CompactData = True
pvtTable.Outline = True
pvtTable.OutlineData = True
pvtTable.ShowHeaders = True
pvtTable.UseAutoFormatting = True
pvtTable.ApplyWidthHeightFormats = True
pvtTable.ShowDrill = True
pvtTable.RowHeaderCaption = "Caption"
' Set the top field
Dim r1 As OfficeOpenXml.Table.PivotTable.ExcelPivotTableField = pvtTable.Fields("FirstField")
r1.Sort = OfficeOpenXml.Table.PivotTable.eSortType.Ascending
pvtTable.RowFields.Add(r1)
' Set the second field
Dim r2 As OfficeOpenXml.Table.PivotTable.ExcelPivotTableField = pvtTable.Fields("SecondField")
r2.Sort = OfficeOpenXml.Table.PivotTable.eSortType.Ascending
pvtTable.RowFields.Add(r2)
r2.ShowAll = False
' Set the DataField
Dim df1 As OfficeOpenXml.Table.PivotTable.ExcelPivotTableField = pvtTable.Fields("DataField")
df1.SubTotalFunctions = OfficeOpenXml.Table.PivotTable.eSubTotalFunctions.Sum
pvtTable.DataFields.Add(df1)
请对这个问题或其他问题提供任何和所有帮助 - 无论是 C# 还是 VB、EPPlus 或 OpenXML - 我只需要让它工作!!!
谢谢!
最佳答案
我相信您想从另一张表中添加数据。我在您的其他帖子中的帖子显示了完整代码 EPPlus Pivot Table - Collapse entire field
var pt = wsPivot1.PivotTables.Add(wsPivot1.Cells["A1"], ws.Cells["K1:N11"], "Pivottable1");
调用“PivoTables.Add()”请参阅下面的工具提示。
// Summary:
// Create a pivottable on the supplied range
//
// Parameters:
// Range:
// The range address including header and total row
//
// Source:
// The Source data range address
//
// Name:
// The name of the table. Must be unique
//
// Returns:
// The pivottable object
关于c# - OpenXML/EPPlus - 在 .Net 中创建 PivotCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346004/