4

我正在将 Excel 互操作数据透视表代码移植到 EPPlus。我在生成计算字段值时遇到了困难。在 Excel 互操作中,我可以这样做:

pvt.AddDataField(pvt.PivotFields("TotalQty"), "Total Packages", XlConsolidationFunction.xlSum).NumberFormat = "###,##0";
pvt.AddDataField(pvt.PivotFields("TotalPrice"), "Total Purchases", XlConsolidationFunction.xlSum).NumberFormat = "$#,##0.00"; 
PivotField avg = pvt.CalculatedFields().Add("Average Price", "=TotalPrice/TotalQty", true);
avg.Orientation = XlPivotFieldOrientation.xlDataField;
avg.NumberFormat = "$###0.00";

这样,数据透视表上的“平均价格”值将显示 TotalPrice 除以 TotalQty 的值。

但是如何在 EPPlus 中做到这一点?我可以像这样创建“普通香草”数据字段:

var totQtyField = pivotTable.Fields["TotalQty"];
pivotTable.DataFields.Add(totQtyField);

var totPriceField = pivotTable.Fields["TotalPrice"];
pivotTable.DataFields.Add(totPriceField);

...但是在计算价值时,我感到困惑。我的出发点是一个在线示例,如下所示:

pivotTable.DataFields.Add(pivotTable.Fields["AvgPrice"]).Function = 
    OfficeOpenXml.Table.PivotTable.DataFieldFunctions.Sum();

所以我尝试了以下方法,但没有一个是正确的:

pivotTable.DataFields.Add(pivotTable.Fields["AvgPrice"]).Function = 
    OfficeOpenXml.Table.PivotTable.DataFieldFunctions.Average(totPriceField, totQtyField);
pivotTable.DataFields.Add(pivotTable.Fields["AvgPrice"]).Function = "TotalPrice/TotalQty";
pivotTable.DataFields.Add(pivotTable.Fields["AvgPrice"]) = "TotalPrice/TotalQty";
pivotTable.DataFields.Add(pivotTable.Fields["AvgPrice"]) = totPriceField / totQtyField;
pivotTable.DataFields.Add(pivotTable.Fields["AvgPrice"]).Function =
    OfficeOpenXml.Table.PivotTable.
    DataFieldFunctions.Product(totPriceField/totQtyField);

这些连枷都没有编译。我需要做哪些调整,或者我需要采取哪些全新的方法?

更新

我可以计算这些值并将它们放在数据表上并以这种方式引用它;但这真的是唯一/最好的方法吗?

更新 2

我这样做了(将 vals 直接添加到包含数据透视表源数据的工作表中):

var avgCell = rawDataWorksheet.Cells[_lastRowAddedRawData + 1, 9];
if ((TotalPrice < 0.0M) || (Quantity < 1))
{
    avgCell.Value = 0.0;
}
else
{
    avgCell.Value = TotalPrice / Quantity;
}

var prcntgCell = rawDataWorksheet.Cells[_lastRowAddedRawData + 1, 10];
if ((TotalPrice < 0.0M) || (MonthYear == String.Empty))
{
    prcntgCell.Value = 0.0;
}
else
{
    prcntgCell.Value = GetPercentageOfItemForMonthYear(TotalPrice, MonthYear);
}

private double GetPercentageOfItemForMonthYear(decimal totPrice, strin
    monthYear)
{
    decimal totalForMonthYear = monthlySales[monthYear];
    double prcntg = GetPercentageOfItem(totPrice, totalForMonthYear);
    return prcntg;
}

private double GetPercentageOfItem(decimal portionOfTotal, decimal  
    grandTotal)
{
    if ((portionOfTotal <= 0.0M) || (grandTotal <= 0.0M))
    {
        return 0.0;
    }
    if (portionOfTotal == grandTotal)
    {
        return 100.0;
    }
    double d = Convert.ToDouble(portionOfTotal) 
        / Convert.ToDouble(grandTotal) * 100;
    return Math.Round(d, 2);
}

...但仍然想知道在创建数据透视表时如何使用计算字段来完成。

4

1 回答 1

5

EPPlus 目前不支持数据透视表中的计算字段。我使用了以下解决方法:

using System.Linq;
using System.Xml;
using OfficeOpenXml.Table.PivotTable;
using ReflectionMagic;

public static ExcelPivotTableField AddCalculatedField(this ExcelPivotTable pivotTable, string calcFieldName, string formula)
{
    var dynamicPivotTable = pivotTable.AsDynamic();
    var maxIndex = pivotTable.Fields.Max(f => f.Index);
    const string schemaMain = @"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
    var cacheTopNode = dynamicPivotTable.CacheDefinition.CacheDefinitionXml.SelectSingleNode("//d:cacheFields", dynamicPivotTable.NameSpaceManager);
    cacheTopNode.SetAttribute("count", (int.Parse(cacheTopNode.GetAttribute("count")) + 1).ToString());
    var cacheFieldNode = dynamicPivotTable.CacheDefinition.CacheDefinitionXml.CreateElement("cacheField", schemaMain);
    cacheFieldNode.SetAttribute("name", calcFieldName);
    cacheFieldNode.SetAttribute("databaseField", "0");
    cacheFieldNode.SetAttribute("formula", formula);
    cacheFieldNode.SetAttribute("numFmtId", "0");
    cacheTopNode.AppendChild(cacheFieldNode);

    var topNode = dynamicPivotTable.PivotTableXml.SelectSingleNode("//d:pivotFields", dynamicPivotTable.NameSpaceManager);
    topNode.SetAttribute("count", (int.Parse(topNode.GetAttribute("count")) + 1).ToString());
    XmlElement fieldNode = dynamicPivotTable.PivotTableXml.CreateElement("pivotField", schemaMain);
    fieldNode.SetAttribute("compact", "0");
    fieldNode.SetAttribute("outline", "0");
    fieldNode.SetAttribute("showAll", "0");
    fieldNode.SetAttribute("defaultSubtotal", "0");
    topNode.AppendChild(fieldNode);

    var excelPivotTableFieldType = typeof(ExcelPivotTableField).AsDynamicType();
    var excelPivotTableField = excelPivotTableFieldType.New((XmlNamespaceManager)dynamicPivotTable.NameSpaceManager, fieldNode, (ExcelPivotTable)dynamicPivotTable, maxIndex + 1, maxIndex + 1);
    excelPivotTableField.SetCacheFieldNode(cacheFieldNode);
    dynamicPivotTable.Fields.AddInternal(excelPivotTableField);

    return pivotTable.Fields.First(f => f.Name == calcFieldName);
}

示例用法:

var calc1 = pivotTable.AddCalculatedField("Calc1", "BCol*BCol");
var dataField = pivotTable.DataFields.Add(calc1);
dataField.Function = DataFieldFunctions.Sum;
dataField.Name = "Override Name";
于 2017-03-23T15:47:10.323 回答