简体   繁体   English

在Epplus中创建没有基表的数据透视表(C#)

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

Can is possibility create Excel document without placed inside spreedsheet the base table? 可以创建Excel文件而不放在spreedsheet基表中吗? Or at least that base table was in other worksheet than pivot table. 或者至少该基表位于其他工作表中而不是数据透视表。

Currently I create DataColumn, after add row and... 目前我在添加行和...后创建DataColumn

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");
pivotTable.RowFields.Add(pivotTable.Fields["Grid"]);
.
.
.

It seems that EPPlus PivotTable can be created only from the data that is already present in the workbook; 似乎只能从工作簿中已存在的数据创建EPPlus数据透视表; 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(
        ws.Cells[1,1], 
        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. 在这种情况下,您可以将基表与C#代码聚合(按用于数据透视表行和列的列进行分组),并使用分组结果作为Excel数据透视表的基表。 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). 可以使用PivotData库简单地执行聚合 - 如果您对此方式感兴趣并需要更多解释(我是此库的作者),请告诉我。

Absolutely! 绝对! Just set the data source of the pivot table using PivotTable.SourceData to specify a connection string. 只需使用PivotTable.SourceData设置数据透视表的数据源即可指定连接字符串。

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.pivottable.sourcedata.aspx https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.pivottable.sourcedata.aspx

A little different way to do it, that works for me, is as follows: 对我有用的一个不同的方法如下:

var dataRange = rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Address];
var pivotTable =
    pivotTableWorksheet.PivotTables.Add(pivotTableWorksheet.Cells["A6"]
        dataRange, "PivotTable");

"rawDataWorksheet" is the sheet that contains the source data; “rawDataWorksheet”是包含源数据的工作表; "pivotTableWorksheet" is the sheet on which you want to plop the PivotTable. “pivotTableWorksheet”是您要在其上填充数据透视表的工作表。 In the code above, it starts at the intersection of column A (1) and row 6, but you could use another target cell, of course. 在上面的代码中,它从列A(1)和第6行的交叉点开始,但是当然可以使用另一个目标单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM