如何隐藏所选范围并对显示的结果进行排序(Aspose Cells)?

时间:2016-11-29 22:46:16

标签: sorting pivot-table aspose aspose-cells sortdirection

我可以使用以下代码按选定的值对显示的结果进行排序(降序):

PivotField field = pivotTable.RowFields[0];
field.IsAutoSort = true;
field.IsAscendSort = false;
field.AutoSortField = 1;

这就是我所看到的(显示的总购买量确实从最多到最少显示):

enter image description here

或者,我只能使用以下代码显示“总百分比”值至少为1%的描述范围:

private void HideItemsWithFewerThan1PercentOfSales()
{
    int FIRST_TOTAL_PRICE_ROW = 8;
    int ROWS_BETWEEN_PERCENTAGES = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_TOTAL_PRICE_ROW;
    int rowsUsed = dataBodyRange.EndRow;

    pivot.RefreshData();
    pivot.CalculateData();

    // Get grand total of purchases for all items and months, and calculate what 1% of that is
    Cell totalTotalPurchasesCell = pivotTableSheet.Cells[rowsUsed - 2, _grandTotalsColumnPivotTable + 1];
    double totalTotalPurchases = Convert.ToDouble(totalTotalPurchasesCell.Value);
    var onePercentOfTotalPurchases = totalTotalPurchases / 100;

    // Loop through PivotTable data, hiding where percentage < 0.01 (1%)
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell priceCell = pivotTableSheet.Cells[currentRowBeingExamined, _grandTotalsColumnPivotTable + 1];
        String priceStr = priceCell.Value.ToString();
        Double price = Convert.ToDouble(priceStr);
        if (price < onePercentOfTotalPurchases)
        {
            pivotTableSheet.Cells.HideRows(currentRowBeingExamined - 1, ROWS_BETWEEN_PERCENTAGES);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_PERCENTAGES;
    }
}

......就像这样:

enter image description here

...但我不能让他们两个同时工作。所以我可以用少于1%的percntage隐藏描述,或者我可以按总购买量下降排序,但我无法同时完成两者。我试图完成两者的代码如下:

. . .
pivotTable.AddFieldToArea(PivotFieldType.Row, DESCRIPTION_COLUMN);
pivotTable.RowHeaderCaption = "Description";

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

// Dragging the third field to the data area.
pivotTable.AddFieldToArea(PivotFieldType.Data, TOTALQTY_COLUMN);
pivotTable.DataFields[0].DisplayName = "Total Packages";

pivotTable.AddFieldToArea(PivotFieldType.Data, TOTALPRICE_COLUMN);
pivotTable.DataFields[1].DisplayName = "Total Purchases";
. . .

// Sort by "Total Purchases" descending
PivotField field = pivotTable.RowFields[0];
field.IsAutoSort = true;
field.IsAscendSort = false;
field.AutoSortField = 1; // This is the "Total Purchases" field

pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;

pivotTable.RefreshDataFlag = true;
pivotTable.RefreshData();
pivotTable.CalculateData();
pivotTable.RefreshDataFlag = false;

List<String> contractItemDescs = GetContractItemDescriptions();
ColorizeContractItemBlocks(contractItemDescs);
HideItemsWithFewerThan1PercentOfSales();
FreezePanePivotTable(HEADER_ROW, 2); 
FormatPivotTableNumbers();
ConfigureForPrinting(pivotTableSheet.Cells.Rows.Count);

当调用HideItemsWithFewerThan1PercentOfSales()时,好像没有遵守排序顺序 - 方法“看到”的行号不是根据已建立的排序的行号。

如何让排序和隐藏工作?

注意:调用HideItemsWithFewerThan1PercentOfSales();在排序代码之前不起作用 - 它仍然显示/隐藏了一些错误的东西。

1 个答案:

答案 0 :(得分:1)

请在Aspose.Cells论坛中查看此thread的回复。

注意: 我在Aspose担任开发人员传播者