Alex Alex - 1 year ago 316
C# Question

Create pivot table without the base table in Epplus (C#)

Can is possibility create Excel document without placed inside spreedsheet the base table? Or at least that base table was in other worksheet than pivot table.

Currently I create DataColumn, after add row and...

ExcelWorksheet worksheet = pkg.Workbook.Worksheets.Add("Table");
worksheet.Cells["A1"].LoadFromDataTable(table, true);
var pivotTable = worksheet.PivotTables.Add(worksheet.Cells["H14"],

worksheet.Cells[rangePivotTable], "pivTable");

Answer Source

It seems that EPPlus PivotTable can be created only from the data that is already present in the workbook; so you have to place source table into the spreadsheet.

Good news is that you can easily have pivot table and base table in the different worksheets:

var wsPvt = pkg.Workbook.Worksheets.Add( "Pivot Table" );
var wsData = pkg.Workbook.Worksheets.Add( "Source Data" );

var rangePivotTable = wsData.Cells["A1"].LoadFromDataTable( tbl, true );

var pivotTable = wsPvt.PivotTables.Add(
        rangePivotTable, "pvtTable");

I assume that you don't want to place base table into spreadsheet because it is either rather big or just contain sensitive details. In this case you may aggregate base table with C# code (group it by columns that used for pivot table rows and columns) and use grouped result as base table for Excel Pivot Table. Aggregation may be simply performed with PivotData library -- let me know if you're interested in this way and need more explanations (I'm an author of this library).