问题
I'm hoping this could help me, at least, answer one of the 2 questions I asked here, but I am looking how to create a PivotCache in EPPlus / OpenXML and can't find anything online / in their documentation that shows how to do it.
So, suposing I have one Excel sheet, wksRawData
created in EPPlus and I want to create a second sheet with a pivot table based upon the pivot cache of wksRawData.Cells(wksRawData.Dimension.Address)
- In the hopes that then I could delete wksRawData but still keep the pivot table. How would I do that?
So far, my code for creating the pivot table in my second worksheet is:
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)
PLEASE, any and all help on this or the other question would REALLY be appreciated - Whether it be in C# or VB, EPPlus or OpenXML - I just need to get this working!!!
THANKS!
回答1:
I belive you want to add data from another sheet. My post in your other thread shows full code EPPlus Pivot Table - Collapse entire field
var pt = wsPivot1.PivotTables.Add(wsPivot1.Cells["A1"], ws.Cells["K1:N11"], "Pivottable1");
calling "PivoTables.Add()" see tool tip below.
// 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
来源:https://stackoverflow.com/questions/16346004/openxml-epplus-create-pivotcache-in-net