为什么具有值的单元格被视为空值(Aspose Cells)?

Why is a cell with a value seen as being null (Aspose Cells)?

我需要遍历数据透视表并为特定范围着色。我正在尝试使用以下代码来做到这一点:

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");

...不过也被废除使徒打倒了

这些调用必须添加到 while 循环之外:

pivot.RefreshData();
pivot.CalculateData();