为什么试图在数据透视表中着色范围没有效果(Aspose Cells)?
我试图有条件地着色数据透视表中的范围,如下所示:
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; } }
…但是,尽pipeif块已经达到好几次(比如“rangeToColorize”跨越了A28:E31),但是它并不“取”。 我的造型或风格标志或应用这些有什么问题?
UPDATE
即使我改变了这个:
// 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);
…基于官方文件 ,它没有任何区别。
更新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 ”为“ ForegroundColor ”,它什么都不做; 有问题的单元格没有着色。
更新3
那么(或者)一件奇怪的事情是我能够上色的唯一方法就是手动生成的“总计”列:
正如你所看到的,某些行已经根据条件进行了着色。 但是,只有在这一栏中,不应该在整个行上,因为它应该(至less在理论上)是:
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();
…我得到,“ 方法没有重载格式”需要0个参数。
…也简单地说:
PivotTable.Format();
(大写“P”,没有“pivotTable”分配),我得到相同的错误信息。
更新5
即使有以下,根据另一个Aspose支持人员的build议,它什么都不做:
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); // " "
…但没有区别。
那么我想知道上面的第一个两行代码是否有必要,但是注释这些代码导致列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; }
那么我如何才能使彩色化涵盖所有栏目,包括“总购买量”,“平均价格总和”和“总计”百分比?
请更改以下两行
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作为Developer Evangelist工作