openxml 更新同一行中的 2 个单元格引发异常
openxml update 2 cells in same row throws an exception
在我的模板中,我喜欢更新同一行中的 2 个单元格。第一个单元格是 A53,第二个单元格是 C53。
例外是这个"Object reference not set to an instance of an object"
我在调用方法"InsertCellInWorksheet"的过程中调用方法"UpdateCell"发生错误。
错误发生在下一行,但仅在调用第二个 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;
}
我的问题是代码哪里有异常?
我想通了。
我改变了这一行
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();
}
在我的模板中,我喜欢更新同一行中的 2 个单元格。第一个单元格是 A53,第二个单元格是 C53。
例外是这个"Object reference not set to an instance of an object"
我在调用方法"InsertCellInWorksheet"的过程中调用方法"UpdateCell"发生错误。
错误发生在下一行,但仅在调用第二个 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;
}
我的问题是代码哪里有异常?
我想通了。
我改变了这一行
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();
}