我已经能够创建一个与原始/源数据分开的数据透视表,但现在我想将两者结合起来,数据透视表允许通过在列标题行上提供过滤器来过滤电子表格数据,如下所示:
我试过这段代码:
private void AddPivotTable()
{
// The commented-out code below placess the PivotTable below the actual data, separate from it:
//string colAlphaRowNum = string.Format("A{0}", locationWorksheet.Dimension.End.Row+5);
// Here I am attempting to incorporate the PivotTable within the data itself (one row above it, actually)
string colAlphaRowNum = "A5";
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.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]);
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
}
...但这不起作用。打开生成的工作表时出现此对话框:
如果我选择“是”,这就是我所看到的:
如果我选择“否”,我会看到:
...这是有希望的,但是如果我然后下拉“行标签”,取消选择“(全选)”,然后选择第一项(“船尾”),我会看到:
这不是我想要的;在模型(手工制作)表中,取消选择“全选”,然后选择单个项目过滤数据以仅包含该数据(在本例中为“Foster”),如下所示:
...而不是用受限数据透视表替换数据的第一部分。
我需要做什么才能使这项工作按预期进行?