将多个单元格添加到单个行 [英] Adding multiple Cells to a single Row

查看:113
本文介绍了将多个单元格添加到单个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此并不陌生,当我尝试在一行中添加多个单元格时,它说存在不可读的内容.这就是我所拥有的.

I am new to this and when I try to add more than one cell to a row it says there is unreadable content. Here is what I have.

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

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

If I remove the second cell, it works as expected.

推荐答案

单元格引用应为"B1"而不是"A2"

Instead of "A2" the cell reference should be "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")
        };

这篇关于将多个单元格添加到单个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆