It doesn’t work for me; if I color everything (as a test), it hoses the data up.
If I try to color just one cell (as a test), it colors row 258. This is the code:
// This colored up row 258…?!?
//var style2 = pivotTableSheet.Cells[9, 1].GetStyle();
//style2.BackgroundColor = Color.Red;
//style2.Pattern = BackgroundType.Solid;
//pivot.Format(9, 1, style2);
// This does the same thing (reds up row 258)
var style2 = pivotTableSheet.Cells[9, 1].GetStyle();
style2.BackgroundColor = Color.Red;
style2.Pattern = BackgroundType.Solid;
pt.Format(9, 1, style2);
…and here is a screenshot of the result (below)
Here is the current complete method:
private void ColorizeContractItemBlocks(List 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();
PivotTable pt = pivotTableSheet.PivotTables[0];
var style = workBook.CreateStyle();
// 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.BackgroundColor = CONTRACT_ITEM_COLOR;
style.Pattern = BackgroundType.Solid;
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);
}
// This colored up row 258 or so…?!?
//var style2 = pivotTableSheet.Cells[9, 1].GetStyle();
//style2.BackgroundColor = Color.Red; // CONTRACT_ITEM_COLOR; //Color.Beige;
//style2.Pattern = BackgroundType.Solid;
//pivot.Format(9, 1, style2);
// This does the same thing (reds up row 258)
var style2 = pivotTableSheet.Cells[9, 1].GetStyle();
style2.BackgroundColor = Color.Red; // CONTRACT_ITEM_COLOR; //Color.Beige;
style2.Pattern = BackgroundType.Solid;
pt.Format(9, 1, style2);
}
currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
}
}