c# - 将多个单元格添加到单个行

标签 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();

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

最佳答案

单元格引用应该是“B1”而不是“A2”

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

关于c# - 将多个单元格添加到单个行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117013/

相关文章:

c# - 段落列表不包含完整内容

c# - 如果我向 AddControllersWithViews() 添加扩展,它是否也适用于 AddRazorPages()

c# - 调试键盘事件,如 'Ctrl+Up Arrow'

C# 阅读器关闭时调用 Read 的尝试无效

c# - OpenXml "Can not access a closed stream"仅在生产中

c# - 使用 Open Xml 将列添加到现有 Excel 2007 工作簿

c# - 删除 OpenXML DOCX 的文档变量?

c# - 不使用 Web 服务的 C# 中的实时股票行情

xml - Microsoft Word (OOXML/DOCX) 文档格式验证

c# - 在 openXML 中添加单元格和行