C# 同一行中的openxml更新2个单元格引发异常
在我的模板中,我喜欢更新同一行中的两个单元格。第一个单元是A53,第二个单元是C53 例外情况是“对象引用未设置为对象的实例” 在发生错误的过程中,我调用方法“UpdateCell”,我调用方法“InsertCellInWorksheet” 错误发生在下一行,但仅在调用第二个UpdateCell方法之后,并且仅当它们位于同一行时才会发生C# 同一行中的openxml更新2个单元格引发异常,c#,openxml,C#,Openxml,在我的模板中,我喜欢更新同一行中的两个单元格。第一个单元是A53,第二个单元是C53 例外情况是“对象引用未设置为对象的实例” 在发生错误的过程中,我调用方法“UpdateCell”,我调用方法“InsertCellInWorksheet” 错误发生在下一行,但仅在调用第二个UpdateCell方法之后,并且仅当它们位于同一行时才会发生 if (row.Elements<Cell>().Where(c => c.CellReference.Value == cellRefere
if (row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).Count() > 0)
“UpdateCell”方法的我的代码:
以及“InsertCellInWorksheet”方法
公共静态单元格InsertCellInWorksheet(字符串列名称、uint行索引、工作表部件工作表部件)
{
工作表=工作表零件工作表;
SheetData SheetData=工作表.GetFirstChild();
字符串cellReference=列名称+行索引;
行行;
if(sheetData.Elements().Where(r=>r.RowIndex==RowIndex).Count()!=0)
{
row=sheetData.Elements()。其中(r=>r.RowIndex==RowIndex)。First();
}
其他的
{
行=新行(){RowIndex=RowIndex};
sheetData.Append(行);
}
if(row.Elements().Where(c=>c.CellReference.Value==CellReference.Count()>0)
{
返回row.Elements(),其中(c=>c.CellReference.Value==CellReference.First();
}
其他的
{
//单元格必须按照CellReference的顺序排列。确定在何处插入新单元格。
Cell refCell=null;
foreach(row.Elements()中的单元格)
{
if(string.Compare(cell.CellReference.Value,CellReference,true)>0)
{
refCell=单元格;
打破
}
}
Cell newCell=newCell(){CellReference=CellReference};
row.InsertBefore(newCell、refCell);
工作表。保存();
返回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().Where(c=>c.CellReference.Value==CellReference.Count()>0)
{
返回row.Elements(),其中(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();
}
if(row.Elements().Where(c=>c.CellReference!=null&&c.CellReference.Value==CellReference.Count()>0)
{
返回row.Elements()。其中(c=>c.CellReference!=null&&c.CellReference.Value==CellReference).Single();
}
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();
}