我有一個相關的問題heretofore,答案那裏(幾乎)對我的作品是使用:如何爲所有相關的行和列着色,而不僅僅是它們的一個子集(Aspose Cells)?
pt.ShowInCompactForm();
在電子表格中我生成,有跨越5行項目的塊 - 第一行是一個描述,它下面的四行是「子行」,包含該項目的詳細數據(即「總包」,「總購買量」,「平均價格總和」和「總計的百分比」)。
在某些情況下,我需要上色的細胞在該地區和我能夠做到這一點,在大多數情況下,用下面的代碼:
private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
int FIRST_DESCRIPTION_ROW = 7;
int DESCRIPTION_COL = 0;
int ROWS_BETWEEN_DESCRIPTIONS = 5;
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;
CellArea columnRange = pt.ColumnRange;
// Using StartColumn-1 instead of StartColumn-1 gives me the "Percentage of Total" data field subrow (but not the others - "Total Packages", "Total Purchases", and "Sum of Average Price")
for (int c = columnRange.StartColumn-1; c <= columnRange.EndColumn; c++)
{
//pt.Format(currentRowBeingExamined-1, c, style); <= Instead of adding the "Description" row, this colors up some unrelated final ("Percentage of Total") data rows
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;
}
}
但它僅適用「大部分,「因爲我無法爲」描述「行着色(例如」AVOCADOS,HASS 70 CT#2「)或列0/A的最後一行,」總計百分比「在這裏看到:
這可能是DESCRIPT離子排最好留下未被污染/未上漆的,但我認爲下面的亞細胞會更好地變色,我不明白他們爲什麼沒有。
我可以防止所有這些子行從通過使用此被着色:
for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)
(即,從「STARTCOLUMN」而不是「STARTCOLUMN-1」防止任何在第0列的循環/ A被彩色化),但對於我來說,當我從一列開始時(僅在0/A)時,只有最後一個子區域纔會變色。