2012-02-02 16 views
2

これは初めてのことです。行に複数のセルを追加しようとすると、読めないコンテンツがあることがわかります。ここに私が持っているものがあります。複数のセルを1つの行に追加する

SpreadsheetDocument ssDoc = SpreadsheetDocument.Create(saveFile, SpreadsheetDocumentType.Workbook); 

// Add a WorkbookPart to the document 
WorkbookPart workbookPart = ssDoc.AddWorkbookPart(); 
workbookPart.Workbook = new Workbook(); 
// Add a WorksheetPart to theWorkbookPart 
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); 
worksheetPart.Worksheet = new Worksheet(new SheetData()); 

Sheets sheets = ssDoc.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets()); 

Sheet sheet = new Sheet() 
{ Id = ssDoc.WorkbookPart.GetIdOfPart(worksheetPart), 
    SheetId = 1, Name = "Sheet1" 
}; 

sheets.Append(sheet); 
Worksheet worksheet = new Worksheet(); 
SheetData sheetData = new SheetData(); 
Row row = new Row(); 

Cell cell = new Cell() 
{ 
    CellReference = "A1", 
    DataType = CellValues.String, 
    CellValue = new CellValue("Cell1") 
}; 

Cell cell2 = new Cell() 
{ 
    CellReference = "A2", 
    DataType = CellValues.String, 
    CellValue = new CellValue("Cell2") 
}; 
row.Append(cell); 
row.Append(cell2); 

sheetData.Append(row); 
worksheet.Append(sheetData); 
worksheetPart.Worksheet = worksheet; 
// Close the document. 
ssDoc.Close(); 

2番目のセルを削除すると、正常に動作します。代わりに、 "A2" セル参照の

答えて

4

は "B1"

 SpreadSheet.Cell cell = new SpreadSheet.Cell() 
     { 
      CellReference = "A1", 
      DataType = SpreadSheet.CellValues.String, 
      CellValue = new SpreadSheet.CellValue("Cell1")     
     }; 

     SpreadSheet.Cell cell2 = new SpreadSheet.Cell() 
     { 
      CellReference = "B1", 
      DataType = SpreadSheet.CellValues.String, 
      CellValue = new SpreadSheet.CellValue("Cell2") 
     }; 
0

する必要があります私は同様の問題がありました。誰かが同じ列にセルを挿入する場合は、同じ列にセルを挿入するために行インデックスを増やし、セル参照を行う必要があります。週末を通して私を見つけました:D

Row row2 = new Row { RowIndex = 2}; 
SpreadSheet.Cell cell2 = new SpreadSheet.Cell() 
    { 
     CellReference = "B1", 
     DataType = SpreadSheet.CellValues.String, 
     CellValue = new SpreadSheet.CellValue("Cell2") 
    }; 

row2.Append(cell2); 
sheetData.Append(row); 

ループを使う方が良いです。