簡體   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