Openxml更新同一行中的2个单元格会抛出异常

本文关键字:2个 单元格 抛出异常 一行 更新 Openxml | 更新日期: 2023-09-27 18:14:41

在我的模板中,我喜欢更新同一行中的2个单元格。第一个细胞是A53,第二个细胞是C53。

例外是"对象引用未设置为对象的实例"

我调用方法"UpdateCell"在过程中,我调用方法"InsertCellInWorksheet"在错误发生的地方。

错误发生在下一行,但只有在第二个UpdateCell方法被调用之后,并且只有当它们在同一行

if (row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).Count() > 0)

我必须提到,在FooterText5[0].CellText数组中是一个电子邮件地址,其中有一个@。

从这里开始

ExcelCreator.UpdateCell(ms, FooterText3[0].CellText, FooterText3[0].RowIndex, "A");
ExcelCreator.UpdateCell(ms, FooterText5[0].CellText, FooterText5[0].RowIndex, "C");

我的"UpdateCell"方法代码:

public static void UpdateCell(Stream template, string cellText, uint rowIndex, string columnName, bool bold = false, int rowHeight = 0)
    {
        // Memorystream of the Template
        using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(template, true))
        {
            // get the first worksheet
            WorksheetPart worksheetPart = spreadSheet.WorkbookPart.WorksheetParts.
            Cell cell = InsertCellInWorksheet(columnName, rowIndex, worksheetPart);
// some other unrelated code

和InsertCellInWorksheet方法

    public static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
        {
            Worksheet worksheet = worksheetPart.Worksheet;
            SheetData sheetData = worksheet.GetFirstChild<SheetData>();
            string cellReference = columnName + rowIndex;

            Row row;
            if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
            {
                row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
            }
            else
            {
                row = new Row() { RowIndex = rowIndex };
                sheetData.Append(row);
            }
if (row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).Count() > 0)
                {
                    return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
                }
                else
                {
                    // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
                    Cell refCell = null;
                    foreach (Cell cell in row.Elements<Cell>())
                    {
                        if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
                        {
                            refCell = cell;
                            break;
                        }
                    }
                    Cell newCell = new Cell() { CellReference = cellReference };
                    row.InsertBefore(newCell, refCell);
                    worksheet.Save();
                    return newCell;
                }

代码有异常的问题在哪里?

Openxml更新同一行中的2个单元格会抛出异常

我终于弄明白了。

我改变了这行

if (row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).Count() > 0)
            {
                return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
            }

if (row.Elements<Cell>().Where(c => c.CellReference != null && c.CellReference.Value == cellReference).Count() > 0)
            {
                return row.Elements<Cell>().Where(c => c.CellReference != null && c.CellReference.Value == cellReference).Single();
            }