我正在使用 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 属性?
最佳答案
施工 pField.SetAttribute("fillDownLabels", "true");
不起作用。
属性 fillDownLabels
应该在属于 ExtensionList 类 ( <ext>
) 的扩展 ( <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);
}
}
关于c# - 数据透视表中的 OpenOfficeXml 重复标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396094/