2013-05-02 58 views
5

我希望這可以幫助我,至少回答我問here的兩個問題之一,但我正在尋找如何在EPPlus/OpenXML中創建PivotCache,在網上找到任何東西/在他們的文檔中顯示如何做到這一點。OpenXML/EPPlus - 在.Net中創建數據透視緩存

所以,suposing我有一個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 - 我只需要得到這個工作!

謝謝!

回答

0

我相信你想從另一張紙上添加數據。我在你的其他線程後顯示完整的代碼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 
+0

我對你在這裏的意義有點困惑......我的挑戰是我想根據第二張表中的數據創建數據透視表,然後刪除第二張表。面臨的挑戰是,如果您通過EPPlus刪除第二張工作表,則在您打開工作簿時數據透視表將變爲空白。我的邏輯是將數據保存在PivotCache中(然後我可以刪除第二張),但我無法想出辦法。 – 2013-05-23 13:06:56

+0

按照您的邏輯從另一個Q開始,我可以編寫workbook_open的代碼,當工作簿打開時,一旦pivotcache由excel自動創建,它將刪除第二個工作表,但在這裏我想了解如何創建實際的緩存。 。 有什麼想法嗎?? /你在暗示什麼? – 2013-05-23 13:07:34