如何将数据字段放在数据透视表 (Aspose Cells) 中的行字段下方?

How can I place my data fields beneath my row field in my PivotTable (Aspose Cells)?

我有一个使用 Excel Interop 创建的数据透视表,它将数据字段值放在行字段值下方,如下所示:

当我使用 Aspose Cells 创建数据透视表时,数据字段是 1:08 PM 11/18/2016 在右侧的列中,而不是在同一列和行值下方:

这是我用来在 Aspose Cells 中生成数据透视表的代码:

private void PopulatePivotTableSheet()
{
    int DESCRIPTION_COLUMN = 1;
    int MONTHYR_COLUMN = 3;
    int TOTALQTY_COLUMN = 4;
    int TOTALPRICE_COLUMN = 5;
    int PERCENTOFTOTAL_COLUMN = 7;
    int AVGPRICE_COLUMN = 10;
    int COLUMNS_IN_DATA_SHEET = 11;

    AddSheetHeadingSectionToPivotTableSheet();

    Aspose.Cells.Pivot.PivotTableCollection pivotTables = pivotTableSheet.PivotTables;
    int colcount = COLUMNS_IN_DATA_SHEET;
    string lastColAsStr = ReportRunnerConstsAndUtils.GetExcelColumnName(colcount);
    int rowcount = sourceDataSheet.Cells.Rows.Count;
    string sourceDataArg = string.Format("sourceDataSheet!A1:{0}{1}", lastColAsStr, rowcount);
    int index = pivotTableSheet.PivotTables.Add(sourceDataArg, "A6", "PivotTableSheet");
    Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];

    pivotTable.DisplayNullString = true;
    pivotTable.NullString = "0";

    // Dragging the first field to the row area.
    pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, DESCRIPTION_COLUMN);
    pivotTable.RowHeaderCaption = "Description";

    // Dragging the second field to the column area.
    pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, MONTHYR_COLUMN);
    pivotTable.ColumnHeaderCaption = "Months";

    // Dragging the third field to the data area.
    pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, TOTALQTY_COLUMN);
    pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, TOTALPRICE_COLUMN);
    pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, AVGPRICE_COLUMN);
    pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, PERCENTOFTOTAL_COLUMN);
}

这是我用来在 Excel Interop 中生成数据透视表的代码:

private void PopulatePivotTableSheet()
{
    var pch = _xlBook.PivotCaches();
    int pivotDataRowsUsed = _xlBook.Worksheets["PivotData"].UsedRange.Rows.Count;
    int pivotDataColsUsed = _xlBook.Worksheets["PivotData"].UsedRange.Columns.Count;
    string lastColWrittenAsAlpha = ReportRunnerConstsAndUtils.GetExcelColumnName(pivotDataColsUsed);
    string endRange = string.Format("{0}{1}", lastColWrittenAsAlpha, pivotDataRowsUsed);

    Range sourceData = _xlBook.Worksheets["PivotData"].Range[string.Format("A1:{0}", endRange)];

    PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
    PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A6"], "PivotTable");

    pvt.MergeLabels = true; // The only thing I noticed this doing was centering the heading labels
    // Although somewhat confusing, these "error" settings actually prevent the "#DIV/0!" from displaying
    pvt.ErrorString = "";
    pvt.DisplayErrorString = true;
    // This one converts what would otherwise be blank into "0" for ints and "[=11=]" for decimal vals
    pvt.NullString = "-";

    var descField = pvt.PivotFields("Description");
    descField.Orientation = XlPivotFieldOrientation.xlRowField;

    var monthField = pvt.PivotFields("MonthYr");
    monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
    monthField.NumberFormat = "MMM yy";

    // This changes the label from "Column Labels"
    pvt.CompactLayoutColumnHeader = "Months";
    // This changes the label from "Row Labels"
    pvt.CompactLayoutRowHeader = "Description";

    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";

    // This looks wrong, but the value is calculated below 
    pvt.CalculatedFields()._Add("PercentOfTotal", "=TotalPrice");
    pvt.AddDataField(pvt.PivotFields("PercentOfTotal"), "Percentage of Total", Type.Missing).NumberFormat = "###.##";

    // These two lines don't seem that they would do so, but they do result in the items 
    // being sorted by (grand) total purchases descending
    var fld = ((PivotField)pvt.PivotFields("Description"));
    fld.AutoSort(2, "Total Purchases");

    // This gets the Pivot Table to what it should be, appearance-wise...
    pvt.DataPivotField.Orientation = XlPivotFieldOrientation.xlRowField;

    // Add calculations to Percentage cells
    int pivotDataSheetRowsUsed = _xlBook.Worksheets["PivotTable"].UsedRange.Rows.Count;
    int pivotDataSheetColsUsed = _grandTotalsColumnPivotTable;
    int FIRST_PERCENTAGE_ROW = 12;
    int FIRST_PERCENTAGE_COL = 2;
    int ROWS_BETWEEN_PERCENTAGES = 5;
    int currentPercentageRow = FIRST_PERCENTAGE_ROW;

    while (currentPercentageRow < pivotDataSheetRowsUsed)
    {
        for (int columnLoop = FIRST_PERCENTAGE_COL; columnLoop <= pivotDataSheetColsUsed; columnLoop++)
        {
            var prcntgCell = (Range)_xlPivotTableSheet.Cells[currentPercentageRow, columnLoop];
            prcntgCell.NumberFormat = "##.#0%";
            if (null == prcntgCell.Value2)
            {
                prcntgCell.Value2 = 0.0;
            }
            else
            {
                prcntgCell.PivotField.Calculation = XlPivotFieldCalculation.xlPercentOfColumn;
            }
        }
        currentPercentageRow = currentPercentageRow + ROWS_BETWEEN_PERCENTAGES;
    }
    FormatPivotTable();
}

我需要在我的 Aspose Cells 代码中更改什么才能左右移动数据字段(无 "Data" 标签)?如果这是 Excel Interop 中的代码:

pvt.DataPivotField.Orientation = XlPivotFieldOrientation.xlRowField;

...什么是 Aspose Cells 等价物?

请尝试这三种方法中的一种,看看哪一种适合您。

//Your pivot table
PivotTable pt = .....

第一

//To show pivot table in compact form
pt.ShowInCompactForm();
pt.RefreshData();
pt.CalculateData();

第二

//To show pivot table in outline form
pt.ShowInOutlineForm();
pt.RefreshData();
pt.CalculateData();

第三

//To show pivot table in tabular form
pt.ShowInTabularForm();
pt.RefreshData();
pt.CalculateData();

注意:我在 Aspose 担任开发人员布道师