2

我正在使用 Epplus 并尝试在表格类型数据透视表中“重复所有项目标签”。我尝试了很多东西,但 EPPlus 库看起来没有办法。我决定 manulplate 数据透视表 xml,我需要在 pivotTableFields 上添加fillDownLabels属性,但我不知道该怎么做。

private void ManuplateXml(OfficeOpenXml.Table.PivotTable.ExcelPivotTable pivotTable)
{

    var xdPivotTable = pivotTable.PivotTableXml;
    var xdPivotFields = xdPivotTable.FirstChild["pivotFields"];
    if (xdPivotFields == null)
        return;

    foreach (XmlElement pField in xdPivotFields)
    {
        pField.SetAttribute("fillDownLabels", "1");
    }
}

我编写了这个方法,它添加了属性,但我的数据透视表仍然没有重复项目标签。xml格式应该如何?如何使用 fillDownLabels 属性?

4

2 回答 2

2

施工pField.SetAttribute("fillDownLabels", "true");不行。属性fillDownLabels应用于<ext>属于 ExtensionList 类 ( ) 的扩展 ( <extLst>)。在我的解决方案下面:

    private void ManipulateXml(OfficeOpenXml.Table.PivotTable.ExcelPivotTable pivotTable)
    {
        var pivotTableXml = pivotTable.PivotTableXml;
        var nsManager = new XmlNamespaceManager(pivotTableXml.NameTable);
        nsManager.AddNamespace("d", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
        nsManager.AddNamespace("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main");
        var topNode = pivotTableXml.DocumentElement;
        var nodes = topNode.SelectNodes("d:pivotFields/d:pivotField[@axis=\"axisRow\"]", nsManager);

        if (nodes == null) return;

        topNode.SetAttribute("updatedVersion", "6");//this line is important!
        foreach (XmlElement node in nodes)
        {
            var element = pivotTableXml.CreateElement("extLst", nsManager.LookupNamespace("d"));
            var ext = pivotTableXml.CreateElement("ext", nsManager.LookupNamespace("d"));
            ext.SetAttribute("uri", "{2946ED86-A175-432a-8AC1-64E0C546D7DE}");
            ext.SetAttribute("xmlns:x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main");
            var fdLabels = pivotTableXml.CreateElement("x14:pivotField", nsManager.LookupNamespace("x14"));
            fdLabels.SetAttribute("fillDownLabels", "1");
            ext.AppendChild(fdLabels);
            element.AppendChild(ext);
            node.AppendChild(element);
        }
    }
于 2017-03-24T14:08:25.680 回答
0

You need to set the fillDownLabels to true. This is per the OpenXML specification for the PivotField element which the fillDownLabels is an attribute of.

To set it to true, use the following convention:

pField.SetAttribute("fillDownLabels", "true");

Also note from the spec that this attribute can be ignored in some situations:

This attribute is ignored when the compact attribute and the outline attribute of the PivotTable ([ISO/IEC-29500-1] section 18.10) field (1) are "true". This attribute is ignored if the PivotTable ([ISO/IEC-29500-1] section 18.10) field (1) is not on the PivotTable ([ISO/IEC-29500-1] section 18.10) row (2) axis or the PivotTable ([ISO/IEC-29500-1] section 18.10) column (2) axis.

To summarize - to get your fillDownLabels to work correctly:

  1. set the fillDownLabels to true - the default is false
  2. make sure the compact attribute of the outline attribute of this pField is set or false (likely the default - check the spec). If this is true, the fillDownLabels attribute is ignored.
  3. Ensure the Pfield is on the row axis or column axis, otherwise fillDownLabels is ignored.

one last note for folks using OpenXMLSDK - to set a BooleanValue attribute you can use the convention:

fillDownLabels = BooleanValue.FromBoolean(true)

于 2017-01-25T22:50:53.830 回答