[英]How can I pre-select the fields to be included in a PivotTable?
I've got this EPPlus code to create a PivotTable: 我已经有了以下EPPlus代码来创建数据透视表:
private void AddPivotTable()
{
string colAlphaRowNum = string.Format("A{0}", locationWorksheet.Dimension.End.Row+5);
ExcelAddressBase eab = locationWorksheet.Cells[colAlphaRowNum];
ExcelRangeBase erb = locationWorksheet.Cells[6, 1, locationWorksheet.Dimension.End.Row, locationWorksheet.Dimension.End.Column];
var pt = locationWorksheet.PivotTables.Add(eab, erb, "Pivotous");
pt.MultipleFieldFilters = true;
pt.RowGrandTotals = true;
pt.ColumGrandTotals = true;
pt.Compact = true;
pt.CompactData = true;
pt.GridDropZones = false;
pt.Outline = false;
pt.OutlineData = false;
pt.ShowError = true;
pt.ErrorCaption = "[error]";
pt.ShowHeaders = true;
pt.UseAutoFormatting = true;
pt.ApplyWidthHeightFormats = true;
pt.ShowDrill = true;
pt.DataOnRows = false;
pt.FirstHeaderRow = 1; // first row has headers
pt.FirstDataCol = 1; // first col of data
pt.FirstDataRow = 2; // first row of data
pt.TableStyle = TableStyles.Medium6; // There is a "custom" and several Dark, Light, and Medium options
}
This kinda-sorta works; 这种有点有效; I get this on the sheet:
我得到这个表:
If I then (manually) select all six available fields in the "PivotTable Field list" in the NE corner of the sheet, the PivotTable changes its appearnce to this: 如果然后(手动)在工作表的NE角的“数据透视表字段列表”中选择所有六个可用字段,则数据透视表会将其外观更改为:
That's pretty good, because the user can "mess around" with it - selecting any subset of available data for each field. 这非常好,因为用户可以“四处寻找”-为每个字段选择可用数据的任何子集。 But I'd like the PivotTable to start off in this state, rather than it be necessary for the user to manually select the fields.
但是我希望数据透视表以这种状态开始,而不是用户必须手动选择字段。
How is that possible? 那怎么可能?
NOTE: I had similar problems with Excel Interop, and discovered that "things" are much easier using EPPlus as contrasted with that; 注意:我在使用Excel Interop时遇到了类似的问题,并且发现使用EPPlus与之相比,“事物”要容易得多。 still, though, programmatically selecting fields seems a challenge...
不过,以编程方式选择字段似乎仍然是一个挑战...
It is much easier than Excel Interop; 它比 Excel的互操作更加容易; here's all it takes:
这就是所有步骤:
pt.RowFields.Add(pt.Fields[0]);
pt.RowFields.Add(pt.Fields[1]);
pt.RowFields.Add(pt.Fields[2]);
pt.RowFields.Add(pt.Fields[3]);
pt.RowFields.Add(pt.Fields[4]);
pt.RowFields.Add(pt.Fields[5]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.