【问题标题】:Why does attempt to color ranges in a PivotTable have no effect (Aspose Cells)?为什么尝试在数据透视表中为范围着色没有效果(Aspose Cells)?
【发布时间】:2016-11-18 19:09:28
【问题描述】:

我正在尝试像这样有条件地为数据透视表中的范围着色:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();
        if (contractItemDescs.Contains(desc))
        {
            // args are firstRow, firstColumn, totalRows, totalColumns
            Range rangeToColorize = pivotTableSheet.Cells.CreateRange(
                currentRowBeingExamined, 0,
                ROWS_BETWEEN_DESCRIPTIONS, _grandTotalsColumnPivotTable + 2);
            Style style = workBook.Styles[workBook.Styles.Add()];
            style.BackgroundColor = CONTRACT_ITEM_COLOR;
            StyleFlag styleFlag = new StyleFlag();
            styleFlag.All = true;
            rangeToColorize.ApplyStyle(style, styleFlag);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

...但是,尽管 if 块多次到达(例如“rangeToColorize”跨越 A28:E31 的地方),但它不会“接受”;我的样式或样式标记或应用这些有什么问题?

更新

即使我把它改成这样:

// Declare a style object.
Style style;

// Create/add the style object.
style = workBook.CreateStyle();

// To Set the fill color of the range, you may use ForegroundColor with
// Solid Pattern setting.
style.BackgroundColor = CONTRACT_ITEM_COLOR;
style.Pattern = BackgroundType.Solid;

// Create a StyleFlag object.
StyleFlag styleFlag = new StyleFlag();
// Make the corresponding attributes ON.
styleFlag.Font = true;
styleFlag.CellShading = true;

// Apply the style to the range.
rangeToColorize.ApplyStyle(style, styleFlag);

...基于the offical docs,没有区别。

更新 2

即使我将有关某个特定单元格的一些非常明确的代码更改为:

cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = workBook.CreateStyle(); // cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
//style.BackgroundColor = CONTRACT_ITEM_COLOR; 
style.ForegroundColor = CONTRACT_ITEM_COLOR;
pivotTableSheet.Cells[4, 0].SetStyle(style);

...IOW,将“style = cell.GetStyle()”更改为“style = workBook.CreateStyle()”和“BackgroundColor em>" 到 "ForegroundColor",它什么也不做;有问题的单元格没有颜色。

更新 3

嗯,(或)奇怪的事情是,我能够为任何东西着色的唯一方法是在手动生成的“总计”列中:

如您所见,某些行已根据条件着色。但仅在该列中,而不是在整行中,因为它应该(至少在理论上)是:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();
        if (contractItemDescs.Contains(desc))
        {
            // args are firstRow, firstColumn, totalRows, totalColumns
            Range rangeToColorize = pivotTableSheet.Cells.CreateRange(
                currentRowBeingExamined, 0,
                ROWS_BETWEEN_DESCRIPTIONS, _grandTotalsColumnPivotTable + 2);

            // Declare a style object.
            Style style;

            // Create/add the style object.
            style = workBook.CreateStyle();
            style.ForegroundColor = CONTRACT_ITEM_COLOR; //Color.Red;
            style.Pattern = BackgroundType.Solid;

            // Create a StyleFlag object.
            StyleFlag styleFlag = new StyleFlag();
            // Make the corresponding attributes ON.
            styleFlag.Font = true;
            styleFlag.CellShading = true;

            // Apply the style to the range.
            rangeToColorize.ApplyStyle(style, styleFlag);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

这让我觉得数据透视表不能着色。然而,即使我尝试像这样为“普通旧”单元格着色:

cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = workBook.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.ForegroundColor = CONTRACT_ITEM_COLOR;

// Create a StyleFlag object.
StyleFlag styleFlag = new StyleFlag();
// Make the corresponding attributes ON.
styleFlag.Font = true;
styleFlag.CellShading = true;

// Apply the style to the cell
pivotTableSheet.Cells[4, 0].SetStyle(style, styleFlag);

...它不起作用 - 没有添加颜色。为什么着色只在一种情况下有效,而在其他情况下无效?

更新 4

有了这个:

PivotTableCollection pivotTables = pivotTableSheet.PivotTables;
PivotTable pivotTable = pivotTables[0];
pivotTable.Format();

...我明白了,“方法 'Format' 没有重载需要 0 个参数。”

...还有就是这样:

PivotTable.Format();

(大写“P”,未分配“数据透视表”),我得到相同的错误消息。

更新 5

即使有以下情况,根据另一位 Aspose 支持人员的建议,它也无济于事:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();
        if (contractItemDescs.Contains(desc))
        {
            Style style;
            style = workBook.CreateStyle();
            style.ForegroundColor = CONTRACT_ITEM_COLOR; //Color.Red;
            style.Pattern = BackgroundType.Solid;

            StyleFlag styleFlag = new StyleFlag();
            styleFlag.Font = true;
            styleFlag.CellShading = true;

            PivotTable pt = pivotTableSheet.PivotTables[0];
            pt.Format(currentRowBeingExamined, 0, style); // test - does not work, either
            pt.Format(currentRowBeingExamined, 1, style); // " "
            //CellArea columnRange = pt.ColumnRange;
            //for (int c = columnRange.StartColumn; c < columnRange.EndColumn; c++)
            //{
            //    //    pt.Format(columnRange.StartRow + 1, c, style);
            //    pt.Format(currentRowBeingExamined, c, style);
            //}
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

更新 6

现在,我已经用以下代码完成了它:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();

        if (contractItemDescs.Contains(desc))
        {
            Style style;
            style = workBook.CreateStyle();
            style.BackgroundColor = CONTRACT_ITEM_COLOR;
            style.Pattern = BackgroundType.Solid;

            StyleFlag styleFlag = new StyleFlag();
            styleFlag.Font = true;
            styleFlag.CellShading = true;

            PivotTable pt = pivotTableSheet.PivotTables[0];
            pt.Format(currentRowBeingExamined, 0, style); 
            pt.Format(currentRowBeingExamined, 1, style);
            CellArea columnRange = pt.ColumnRange;
            for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)
            {
                pt.Format(currentRowBeingExamined, c, style);
                pt.Format(currentRowBeingExamined+1, c, style);
                pt.Format(currentRowBeingExamined+2, c, style);
                pt.Format(currentRowBeingExamined+3, c, style);
            }
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

...但是 Data 列的第二、第三和第四行没有着色:

为什么不呢?我该如何解决?

更新 7

我尝试让 B 列的元素着色,并尝试更改:

pt.Format(currentRowBeingExamined, 0, style); 
pt.Format(currentRowBeingExamined, 1, style);

...到这个:

pt.Format(currentRowBeingExamined, 0, style); 
pt.Format(currentRowBeingExamined, 1, style);
pt.Format(currentRowBeingExamined, 2, style); // <= made no difference
pt.Format(currentRowBeingExamined, 3, style); // " "

...但它没有任何区别。

然后我想知道上面的前两行 sn-p 是否必要,但是将这些行注释掉会导致 A/0 和 B/1 列根本不着色:

这是当前上下文中的代码:

PivotTable pt = pivotTableSheet.PivotTables[0];
var style = workBook.CreateStyle();

while (currentRowBeingExamined < rowsUsed)
{
    Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
    String desc = descriptionCell.Value.ToString();

    if (contractItemDescs.Contains(desc))
    {
        style.BackgroundColor = CONTRACT_ITEM_COLOR;
        style.Pattern = BackgroundType.Solid;

        pt.Format(currentRowBeingExamined, 0, style); 
        pt.Format(currentRowBeingExamined, 1, style);
        //pt.Format(currentRowBeingExamined, 2, style); <= made no difference
        //pt.Format(currentRowBeingExamined, 3, style);
        CellArea columnRange = pt.ColumnRange;
        for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)
        {
            pt.Format(currentRowBeingExamined, c, style);
            pt.Format(currentRowBeingExamined+1, c, style);
            pt.Format(currentRowBeingExamined+2, c, style);
            pt.Format(currentRowBeingExamined+3, c, style);
        }
    }
    currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
}

那么我怎样才能使颜色跨越所有列,包括“总购买”、“平均价格总和”和“总百分比”单元格?

【问题讨论】:

    标签: excel pivot-table aspose-cells


    【解决方案1】:

    请更改以下两行

    Style style = workBook.CreateStyle();
    style.BackgroundColor = CONTRACT_ITEM_COLOR;
    

    进入

    Style style = workBook.CreateStyle();
    style.ForegroundColor= CONTRACT_ITEM_COLOR;
    

    它应该可以解决您的问题。让我们知道您的反馈。

    更新 2

    请尝试以下两种专门用于数据透视表的方法之一

    PivotTable.Format()
    PivotTable.FormatAll()
    

    更新 5

    对于 PivotTable.Format(),您应该使用 Style.BackgroundColor 属性而不是 Style.ForegroundColor 属性。所以换行

    style.ForegroundColor = CONTRACT_ITEM_COLOR; //Color.Red;
    

    到

    style.BackgroundColor = CONTRACT_ITEM_COLOR; //Color.Red;
    

    它应该可以解决您的问题。谢谢。

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

    【讨论】:

    • 请看我的更新 4
    • 在使用PivotTable.Format()和PivotTable.FormatAll()方法时需要指定相关参数,参考你的论坛帖子回复:aspose.com/community/forums/permalink/811248/810468/…
    • 请查看我的 Update-5。谢谢。
    • 谢谢;几乎可以工作,但请参阅更新 6。
    • FormatAll() 正是这样做的,因此,虽然它确实为这些单元格着色,但它也为所有其他单元格着色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多