【发布时间】:2016-11-19 03:24:09
【问题描述】:
我需要遍历数据透视表并对某些范围进行着色。我正在尝试使用以下代码:
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];
// DataBodyRange returns CellArea that represents range between the header row & insert row
var dataBodyRange = pivot.DataBodyRange;
int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
int rowsUsed = dataBodyRange.EndRow;
// 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 + 1);
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;
}
}
问题是它在下面的第二行崩溃,因为“descriptionCell”被认为是一个空值:
Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
String desc = descriptionCell.Value.ToString();
当 currentRowBeingExamined 的值为 7 时,它第一次通过循环崩溃。请记住 Aspose Cells 的行和列是从 0 开始的,您可以看到“A8”(用电子表格的说法),它对应于第 7 行第 0 列,根据 Aspose Cells 的说法,确实有一个值,即“ANISE, FENNEL 12 CT”:
遍历它,我看到,“A8; ValueType: IsNull”
那么为什么对 descriptionCell 的赋值是 null 而不是“ANISE, FENNEL 12 CT”?
注意:ColorizeContractItemBlocks() 在生成数据透视表后调用。如果我注释掉对 ColorizeContractItemBlocks() 的调用,则会生成带有数据透视表的电子表格,如上面的屏幕截图所示。
更新
只是为了它的 Hec Ramsey,我尝试向下抓住一排,向右抓住一列:
Cell descriptionCell = pivotTableSheet.Cells[8, 1]; // should be "Sum of TotalPrice"
Cell descriptionCell = pivotTableSheet.Cells[7, 1]; // should be "Sum of TotalQty"
...但两者都为空;数据透视表中的任何东西是否被视为不为空?
更新 2
当我尝试这个时,我以为我在做某事(而不是做某事):
Cell descCell = pivot.GetCellByDisplayName("A8");
...但它也被废止的使徒击倒了。
【问题讨论】:
标签: excel pivot-table aspose-cells