繁体   English   中英

如何用不同的值代替自动生成的总计值(Aspose Cells)?

[英]How can I replace automatically-generated grand total values with different ones (Aspose Cells)?

我的电子表格会自动生成一个“总计”列作为最右边的列:

在此处输入图片说明

总的来说,这很好。 但具体来说,我遇到了几个问题:最后两个值(不幸的标签是“平均价格总和”和“百分比总和”)提供了-前几列的总和。 在这些情况下,我不希望有一个简单的总和,但在第一种情况下是平均值,在第二种情况下是百分比。

对于AvgPrice,我需要在“总计”列中计算“总价之和” /“总数量之和”。 例如,第一个AvgPrice总计值应为“ 33.14”,而不是“ 66.26”

对于百分比,我需要项目/说明的总价格(例如,在上面的第一项中看到的“ 25151.75”)与“总价格总计”总计行中的“总价格”值的百分比/列(“ 1529802.82”)。 该值在这里看到:

在此处输入图片说明

因此,第一项(“ ASPARAGUS,大11/1#”)的“百分比”值应约为1.6(因为25151.75约为1529802.82的1/60),而不是1.36。

有没有一种方法可以将其设置为自动在“总计”列中生成这些值,还是需要防止像这样生成“总计”列:

pivotTable.ColumnGrand = false;

...然后将该列手动添加到工作表中,用代码进行计算,然后以这种方式添加这些值?

为了详细调查此问题,请使用示例excel文件在Aspose.Cells论坛中发布查询。 您可以向我们提供源excel文件,实际输出excel文件以及预期的excel文件和示例代码。 像您提供的屏幕截图也将有所帮助。 感谢您在这方面的合作。

Aspose.Cells论坛链接:

https://www.aspose.com/community/forums/aspose.cells-product-family/19/showforum.aspx

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

我认为,最简单的方法是手动添加该列,然后计算必要的值; 这是我现在的操作方式(基本上与Excel Interop中的想法相同-手动添加“总计”列):

在数据透视表代码之后,我调用AddManualGrandTotalColumn(),它是:

private void AddManualGrandTotalColumn()
{
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int rowsUsed = dataBodyRange.EndRow;
    int FIRST_DATA_ROW = 7;
    int currentQtyRow = FIRST_DATA_ROW;
    int ROWS_IN_A_RANGE = 4;

    // Needed?
    pivot.RefreshData();
    pivot.CalculateData();

    // Get Total Sales value, which will be needed for computing the % val
    Cell totalTotalPurchasesCell = pivotTableSheet.Cells[rowsUsed - 2, _grandTotalsColumnPivotTable + 1];
    decimal totalTotalPurchases = Convert.ToDecimal(totalTotalPurchasesCell.Value);

    Cell gtLabelCell = pivotTableSheet.Cells[6, _grandTotalsColumnPivotTable + 2];
    gtLabelCell.Value = "Grand Total";

    Cell QtyCell = null;
    Cell PriceCell = null;
    Cell AvgPriceCell = null;
    Cell PercentageCell = null;
    while (currentQtyRow < rowsUsed)
    {
        // SumTotalQty
        int qty = GetSumTotalQty(currentQtyRow);
        QtyCell = pivotTableSheet.Cells[currentQtyRow, _grandTotalsColumnPivotTable + 2];
        QtyCell.Value = qty;
        // SumTotalPrice
        decimal price = GetSumTotalPrice(currentQtyRow+1);
        PriceCell = pivotTableSheet.Cells[currentQtyRow+1, _grandTotalsColumnPivotTable + 2];
        PriceCell.Value = price;
        // Calculate Avg Price (SumTotalPrice / SumTotalQty)
        decimal avg = 0.0M;
        if ((price > 0) && (qty > 0))
        {
            avg = price / qty;
        }
        AvgPriceCell = pivotTableSheet.Cells[currentQtyRow+2, _grandTotalsColumnPivotTable + 2];
        AvgPriceCell.Value = avg;
        // Calculate Percentage (totalTotalPurchases / SumTotalPrice?)
        decimal prcntg = 0.0M;
        if ((totalTotalPurchases > 0) && (price > 0)) // ? Right calculation?
        {
            prcntg = totalTotalPurchases / price;
        }
        PercentageCell = pivotTableSheet.Cells[currentQtyRow+3, _grandTotalsColumnPivotTable + 2];
        PercentageCell.Value = prcntg;

        currentQtyRow = currentQtyRow + ROWS_IN_A_RANGE;
    }
}

private int GetSumTotalQty(int currentQtyRow)
{
    int FIRST_MONTH_COL = 2;
    int LAST_MONTH_COL = _grandTotalsColumnPivotTable; // - 1;
    int cumulativeQty = 0;
    Cell qtyCell = null;
    for (int i = FIRST_MONTH_COL; i <= LAST_MONTH_COL; i++)
    {
        qtyCell = pivotTableSheet.Cells[currentQtyRow, i];
        cumulativeQty = cumulativeQty + Convert.ToInt32(qtyCell.Value);
    }
    return cumulativeQty;
}

(对于GetSumTotalPrice()等)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM