将多个单元格添加到单个行

Ree*_*eed 3 c# openxml openxml-sdk

我是新手,当我尝试在一行中添加多个单元格时,它表示内容不可读.这就是我所拥有的.

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();
Run Code Online (Sandbox Code Playgroud)

如果我删除第二个单元格,它按预期工作.

小智 5

而不是"A2",单元格引用应为"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")
        };
Run Code Online (Sandbox Code Playgroud)