简体   繁体   English

如何预选要包含在数据透视表中的字段?

[英]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.

相关问题 如何在“ SaveFileDialog”中“预选择”文件 - How to “pre-select” a file in a SaveFileDialog 如何在GridView中预先选择RadioButtonList中的值 - How to pre-select a value in RadioButtonList inside a GridView 使用C#从ASP.NET中的数据库字段中预选择复选框 - Pre-select checkBox from database fields in asp.net using c# 如何在C#/ XAML windows 8应用程序中预先选择多个listview / gridview项目? - How to pre-select multiple listview/gridview items in C#/XAML windows 8 app? 在编辑模式下在GridView的下拉列表中预先选择一个值 - Pre-select a value in DropDown list of a GridView in editing mode MVC3级联DropDownList从数据库中预选择值 - MVC3 Cascade DropDownList pre-select values from database 根据数据库值预选列表框中的多个项目 - Pre-select multiple items in listbox based on database values 如果由包括的实体订购,如何在Linq Select Distinct中订购? - How can I order in Linq Select Distinct if ordered by an Included entity? 如何根据DataField PivotField的内容对数据透视表进行排序/排序? - How can I order/sort a PivotTable based on the contents of a DataField PivotField? 根据查询结果中的记录预先选择多选列表框中的值 - Pre-select the values in a multi-select listbox based on records in query results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM