如何将数据透视表权限合并到工作表中的源数据中(使用EPPlus)?

时间:2016-08-05 16:11:32

标签: excel pivot-table epplus epplus-4

我已经能够创建与原始/源数据分开的数据透视表,但现在我想将两者结合起来,使用数据透视表,通过在列标题行上提供过滤器来过滤电子表格数据,就像这样:

enter image description here

我试过这段代码:

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
}

......但这不起作用。当我打开生成的工作表时,我得到了这个对话框:

enter image description here

如果我选择"是"这就是我所看到的:

enter image description here

如果我选择"否",我会看到:

enter image description here

......这很有希望,但是如果我再下载"行标签",则取消选择"(全选)"然后选择第一个项目(" Stern"),我看到了:

enter image description here

这不是我想要的;在模型(手工制作)表中,取消选择"全选"然后选择一个项目过滤数据以仅包含该数据(" Foster"在这种情况下),如下所示:

enter image description here

...而不是用受限制的数据透视表替换数据的第一部分。

我需要做些什么才能使其按预期工作?

1 个答案:

答案 0 :(得分:0)

也许我的命名是错误的,因为我认为我真正想要的不一定是数据透视表,而是过滤的能力。

而且,虽然尝试这样做,这似乎是合乎逻辑的,甚至在理论上是正确的:

using (var shortNameCell = locationWorksheet.Cells[rowToPop, SHORTNAME_BYDCBYLOC_COL])
{
    shortNameCell.Value = "Short Name";
    shortNameCell.Style.WrapText = false;
    shortNameCell.Style.Font.Size = 12;
    shortNameCell.AutoFilter = true;
}
using (var companyNameCell = locationWorksheet.Cells[rowToPop, COMPANYNAME_BYDCBYLOC_COL])
{
    . . .
    companyNameCell.AutoFilter = true;
}
using (var reasonDescCell = locationWorksheet.Cells[rowToPop, REASONDESC_BYDCBYLOC_COL])
{
    . . .
    reasonDescCell.AutoFilter = true;
}
using (var transTypeCell = locationWorksheet.Cells[rowToPop, TRANSTYPE_BYDCBYLOC_COL])
{
    . . .
    transTypeCell.AutoFilter = true;
}

...只导致最后一列被指定为运动过滤能力,以下是所有四项的工作:

locationWorksheet.Cells["A6:D6"].AutoFilter = true;

使用最后一个,我得到以下内容:

enter image description here

更新

毕竟我需要的是一个数据透视表,我在自动回答here中显示了我如何开始如何完成我需要的工作。