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

时间:2016-11-18 21:13:35

标签: pivot-table aspose-cells

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

enter image description here

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

enter image description here

以下是我用于在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 "$0" 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代码中需要更改什么才能左右移动数据字段(无“数据”标签)?如果这是Excel Interop中执行此操作的代码:

pvt.DataPivotField.Orientation = XlPivotFieldOrientation.xlRowField;

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

1 个答案:

答案 0 :(得分:1)

请尝试以下三种方法中的一种,看看它对你有用。

//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担任开发人员传播者