简体   繁体   English

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

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

My spreadsheet automatically generates a "Grand Totals" column as the rightmost column: 我的电子表格会自动生成一个“总计”列作为最右边的列:

在此处输入图片说明

This is nice, in general. 总的来说,这很好。 But in specific, I've got a couple of problems with it: The last two values (with the unfortunate labels "Sum of Avg Price" and "Sum of Percentage") provide just that - a sum of the previous columns. 但具体来说,我遇到了几个问题:最后两个值(不幸的标签是“平均价格总和”和“百分比总和”)提供了-前几列的总和。 In those cases, I don't want a simple sum, but an average in the first case and a percentage in the second case. 在这些情况下,我不希望有一个简单的总和,但在第一种情况下是平均值,在第二种情况下是百分比。

For the AvgPrice, what I need is a calculation of "Sum of Total Price" / "Sum of Total Quty" in the Grand Total column. 对于AvgPrice,我需要在“总计”列中计算“总价之和” /“总数量之和”。 For instance, the first AvgPrice Grand Total value should be "33.14" rather than "66.26" 例如,第一个AvgPrice总计值应为“ 33.14”,而不是“ 66.26”

For the Percentage, I need the percentage of Total Price for the item/Description (such as "25151.75" seen in the first item above) as compared to the "Total Price" value in the "Total Sum of Total Price" grand total row/column ("1529802.82"). 对于百分比,我需要项目/说明的总价格(例如,在上面的第一项中看到的“ 25151.75”)与“总价格总计”总计行中的“总价格”值的百分比/列(“ 1529802.82”)。 That value is seen here: 该值在这里看到:

在此处输入图片说明

So the "Percentage" value for that first item ("ASPARAGUS, LARGE 11/1#") should be approximately 1.6 (as 25151.75 is about 1/60th of 1529802.82), rather than 1.36. 因此,第一项(“ ASPARAGUS,大11/1#”)的“百分比”值应约为1.6(因为25151.75约为1529802.82的1/60),而不是1.36。

Is there a way to set this up to automatically generate those values in the Grand Total Column, or do I need to prevent the Grand Total column from being generated like so: 有没有一种方法可以将其设置为自动在“总计”列中生成这些值,还是需要防止像这样生成“总计”列:

pivotTable.ColumnGrand = false;

...and then add that column to the sheet manually, doing the calculations in code, and adding those values that way? ...然后将该列手动添加到工作表中,用代码进行计算,然后以这种方式添加这些值?

In order to look into this issue in detail, please post your query in Aspose.Cells forum with your sample excel files. 为了详细调查此问题,请使用示例excel文件在Aspose.Cells论坛中发布查询。 You can provide us source excel file, actual output excel file and the expected excel file and sample code. 您可以向我们提供源excel文件,实际输出excel文件以及预期的excel文件和示例代码。 And the screenshot like you provided will also be helpful. 像您提供的屏幕截图也将有所帮助。 Thanks for your cooperation in this regard. 感谢您在这方面的合作。

Aspose.Cells Forum Link: Aspose.Cells论坛链接:

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

Note: I am working as Developer Evangelist at Aspose 注意: 我在Aspose担任开发人员布道者

It's easiest, I think, to just add that column manually, calculating the necessary values; 我认为,最简单的方法是手动添加该列,然后计算必要的值; here's how I'm doing it now (basically the same idea as in Excel Interop - manually adding the Grand Total column): 这是我现在的操作方式(基本上与Excel Interop中的想法相同-手动添加“总计”列):

After the PivotTable code, I call AddManualGrandTotalColumn(), which is: 在数据透视表代码之后,我调用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;
}

. . . (etc. for GetSumTotalPrice()) (对于GetSumTotalPrice()等)

暂无
暂无

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

相关问题 如何在计算出的像元(Aspose Cells)中防止“#Div / 0!”? - How can I prevent “#Div/0!” in calculated cells (Aspose Cells)? 如何提供基于Aspose.Cells数据透视表中其他两个值的计算值? - How can I provide a calculated value that is based on two other values in an Aspose.Cells PivotTable? 如何在电子表格范围(Aspose Cells)周围添加边框? - How can I add borders around a spreadsheet range (Aspose Cells)? 如何自动删除Excel中包含“总计”的所有行? - How to delete all row which contains “Grand Total” in Excel automatically? 如何确定数据透视表生成多少行(Aspose Cells)? - How can I determine how many rows a PivotTable generates (Aspose Cells)? 如何覆盖数据透视表(设置单元格)上的列标签文本? - How can I override the column label text on a Pivot Table (Aspose Cells)? 将图像放在电子表格(Aspose Cells)上时,如何防止图像尺寸改变? - How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)? 如何将电子表格中的列宽设置为特定的像素数(Aspose Cells)? - How can I set the column width in a spreadsheet to a specific pixel count (Aspose Cells)? 如何使用Aspose Cells将数据透视表放在不同的工作表上? - How is it possible to put a PivotTable on a different sheet using Aspose Cells? 如何用空白单元格替换“ 0”值? - How do I replace '0' values with blank cells?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM