我有一个相关的问题heretofore,那里(非常)对我有用的答案就是使用:
pt.ShowInCompactForm();
在我生成的电子表格中,有一些项目块跨越5行 - 第一行是描述,下面的四行是"子行"包含该项目的详细数据(即"总包数","总购买数量","平均价格总和"和"总数百分比&# 34。)
在某些情况下,我需要对该区域中的单元格进行着色,并且能够在大多数情况下使用以下代码执行此操作:
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;
}
}
但它只适用于#34;在大多数情况下,&#34;因为我无法着色&#34;描述&#34;行(例如,&#34; AVOCADOS,HASS 70 CT#2&#34;)或除0/A列的最后一行之外的任何行, - &#34;总和的百分比&#34; subrow,如下所示:
可能是描述行最好没有染色/未上漆,但我认为下面的子行会更好地着色,我不明白为什么它们不是。
我可以使用以下方法阻止所有这些子行被着色:
for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)
(也就是说,用&#34开始循环; StartColumn&#34;而不是&#34; StartColumn-1&#34;防止0 / A列中的任何内容被着色),但它似乎很奇怪当我从一列开始(0 / A)时,只有最后一个子行才会着色。