我试图有条件地为数据透视表中的范围着色,如下所示:
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块被多次到达(例如&#34; rangeToColorize&#34;跨越A28:E31)但它并没有&#34;采取&#34 ;;我的造型或风格标志或应用那些有什么问题?
即使我将其更改为:
// 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,它没有任何区别。
即使我将关于一个特定单元格的一些非常明确的代码更改为:
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,改变&#34; style = cell.GetStyle()&#34; to&#34; style = workBook.CreateStyle()&#34;和&#34; BackgroundColor &#34; to&#34; ForegroundColor &#34;,它什么都不做;有问题的细胞没有着色。
嗯,(或者)奇怪的是,我能够为任何颜色着色的唯一方法是手动生成&#34; Grand Total&#34;柱:
如您所见,某些行已根据条件进行着色。但只有在那一列,而不是整个行,因为它应该(理论上,至少)是:
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;
}
}
因此,我认为数据透视表无法着色。然而,即使我试图为一个普通的老年人着色&#34;像这样的细胞:
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);
......它不起作用 - 不添加颜色。为什么着色只适用于一种情况,而不是其他情况?
有了这个:
PivotTableCollection pivotTables = pivotTableSheet.PivotTables;
PivotTable pivotTable = pivotTables[0];
pivotTable.Format();
...我明白,&#34; 方法&#39;格式&#39;没有重载需要0个参数。&#34;
......还有这个:
PivotTable.Format();
(首都&#34; P&#34;,没有&#34; pivotTable&#34;已分配),我得到同样的错误信息。
即使有以下内容,根据另一位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;
}
}
我现在使用以下代码完成了它的工作:
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;
}
}
...但数据列的第二行,第三行和第四行未着色:
为什么不呢?我该如何解决?
我试图让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;
}
那么我怎样才能让颜色化遍及所有列,包括&#34; Total Purchases&#34;,&#34;平均价格总和&#34;和&# 34;总数百分比&#34;细胞
答案 0 :(得分: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担任开发人员传播者