EPPlus:数据透视表列中的值

时间:2018-06-07 10:55:20

标签: c# epplus epplus-4

在Excel中创建数据透视表并向Rows和Values添加字段时,GUI中会有一个额外的字段[Symbol Sigma] Values,您可以将其拖放到列中。

我正在使用pivot table创建EPPlus。如何将此[Symbol Sigma] Values字段添加到我的列字段?

编辑:这是一些代码。我不知道这应该如何帮助,它被要求的是什么。

private static ExcelWorksheet CreatePivotWorksheet(ExcelPackage excel, ExcelWorksheet dataWorksheet)
{
    string worksheetName = "Pivot";

    ExcelWorksheet pivotWorksheet = excel.Workbook.Worksheets.Add(worksheetName);

    ExcelRangeBase dataRange = dataWorksheet.Cells[dataWorksheet.Dimension.Address];
    ExcelPivotTable pivotTable = pivotWorksheet.PivotTables.Add(pivotWorksheet.Cells[1,1], dataRange, "pivotTable");

    pivotTable.RowGrandTotals = false;

    pivotTable.RowFields.Add(pivotTable.Fields["BaseValue"]);
    pivotTable.RowFields.Add(pivotTable.Fields["Remaining Runtime"])
        .AddNumericGrouping(0, 500000, 30);
    pivotTable.RowFields.Add(pivotTable.Fields["Emittent"]);
    pivotTable.RowFields.Add(pivotTable.Fields["CountIfs"]);
    pivotTable.RowFields.Add(pivotTable.Fields["ISIN"]);

    var stressField = pivotTable.DataFields.Add(pivotTable.Fields["StressScenario - Percent"]);
    stressField.Function = DataFieldFunctions.Average;
    stressField.Format = "0.00%";
    var pessimisticField = pivotTable.DataFields.Add(pivotTable.Fields["PessimisticScenario - Percent"]);
    pessimisticField.Function = DataFieldFunctions.Average;
    pessimisticField.Format = "0.00%";
    var mediumField = pivotTable.DataFields.Add(pivotTable.Fields["MediumScenario - Percent"]);
    mediumField.Function = DataFieldFunctions.Average;
    mediumField.Format = "0.00%";
    var optimisticField = pivotTable.DataFields.Add(pivotTable.Fields["optimisticScenario - Percent"]);
    optimisticField.Function = DataFieldFunctions.Average;
    optimisticField.Format = "0.00%";

    // remove subtotals, this has to be done _after_ adding the field (see https://stackoverflow.com/a/34768357/5909613)
    foreach (ExcelPivotTableField rowField in pivotTable.RowFields)
    {
        rowField.SubTotalFunctions = eSubTotalFunctions.None;
    }
    return pivotWorksheet;
}

What I want to achieve

3 个答案:

答案 0 :(得分:1)

尝试设置pivotTable.DataOnRows = false以获得这种数据透视表行为

答案 1 :(得分:1)

尝试将“值行”更改为列。这是3个值字段索引。

  PivotField row = (PivotField)oPivotTable.RowFields[3];
  row.Orientation = XlPivotFieldOrientation.xlColumnField;

答案 2 :(得分:0)

我为您的案件找到了解决方案。 您应该在代码中为数据字段添加几个字符串。 数据字段“ stressField”的示例:

    stressField.Field.Outline = true;
    stressField.Field.ShowInFieldList=true;

以及其他:

    pessimisticField.Field.Outline = true;
    pessimisticField.Field.ShowInFieldList=true;

    mediumField.Field.Outline = true;
    mediumField.Field.ShowInFieldList=true;

    optimisticField.Field.Outline = true;
    optimisticField.Field.ShowInFieldList=true;