我如何为所有相关的行和列着色,而不仅仅是它们的一个子集(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列的最后一行外, “总计”的百分比如下图所示:

在这里输入图像说明

这可能是描述行最好留下没有被污染的/未涂油漆的,但是我认为下面的这些子箭头会更好的变色,我不明白为什么它们不是。

我可以通过使用这个来防止所有这些子类被着色:

 for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++) 

(也就是说,使用“StartColumn”而不是“StartColumn-1”来启动循环可以防止0 / A列中的任何内容被着色),但对于我来说,当我从一开始就只有最后一个subrow列回(在0 / A)。

我们将在您创build的这个Aspose.Cells论坛主题中查看您的问题。

注意:我在Aspose作为Developer Evangelist工作