【问题标题】:OpenOfficeXml Repeat Labels in Pivot TableOpenOfficeXml 在数据透视表中重复标签
【发布时间】:2017-03-16 17:14:47
【问题描述】:

我正在使用 Epplus 并尝试在表格类型数据透视表中“重复所有项目标签”。 我尝试了很多东西,但 EPPlus 库看起来没有办法。我决定手动制作数据透视表 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 属性?

【问题讨论】:

    标签: c# openxml epplus epplus-4


    【解决方案1】:

    构造pField.SetAttribute("fillDownLabels", "true"); 不起作用。 属性 fillDownLabels 应该用于属于 ExtensionList 类 (<extLst>) 的扩展 (<ext>)。下面是我的解决方案:

        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);
            }
        }
    

    【讨论】:

      【解决方案2】:

      您需要将fillDownLabels 设置为true。这是根据OpenXML specification for the PivotField element which the fillDownLabels is an attribute of

      要将其设置为 true,请使用以下约定:

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

      从规范中还注意到,在某些情况下可以忽略此属性:

      当数据透视表([ISO/IEC-29500-1] 第 18.10 节)字段 (1) 的紧凑属性和大纲属性为“真”时,忽略此属性。如果数据透视表([ISO/IEC-29500-1] 第 18.10 节)字段 (1) 不在数据透视表([ISO/IEC-29500-1] 第 18.10 节)行 (2) 轴或数据透视表上,则忽略此属性([ISO/IEC-29500-1] 第 18.10 节)第 (2) 列轴。

      总结 - 让您的 fillDownLabels 正常工作:

      1. fillDownLabels 设置为true - 默认为false
      2. 确保此 pField 的 outline 属性的 compact 属性已设置或为 false(可能是默认值 - 检查规范)。如果这是true,则fillDownLabels 属性被忽略
      3. 确保 Pfield 位于行轴或列轴上,否则 fillDownLabels 将被忽略

      给使用 OpenXMLSDK 的人的最后一个注意事项 - 要设置 BooleanValue 属性,您可以使用约定:

      fillDownLabels = BooleanValue.FromBoolean(true)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        • 1970-01-01
        相关资源
        最近更新 更多