2016-10-25 3 views
1

私のテンプレートでは、同じ行の2つのセルを更新したい。第1の細胞はA53であり、第2の細胞はC53である。openxml update同じ行の2つのセルが例外をスローする

例外は私は方法エラーが発生した「InsertCellInWorksheet」と呼ぶproccessに「てupdateCell」メソッドを呼び出し、この「オブジェクトのインスタンスに設定されていないオブジェクト参照」

あります。

エラーが行を次のように起こるが、唯一の二てupdateCell方法が

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

と呼ばれ、それらが同じライン上にあるときにのみされた後、私はFooterText5[0].CellText配列にemailadressで、持っていることを言及する必要がありその中に@。

私は

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; 
       } 

されます私の問題は、コードには例外がありますか?

答えて

1

私はそれを理解することができました。

私はこの

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<Cell>().Where(c => c.CellReference.Value == cellReference).Count() > 0) 
      { 
       return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First(); 
      } 

この行を変更

関連する問題