【问题标题】:Adding multiple Cells to a single Row将多个单元格添加到单个行
【发布时间】:2012-02-25 09:12:54
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# openxml openxml-sdk


    【解决方案1】:

    单元格引用应该是“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")
            };
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。如果有人想在同一列中插入单元格,则需要增加行索引和单元格引用才能在同一列中插入单元格。我花了整个周末才弄清楚: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);
      

      最好使用循环。

      【讨论】:

        猜你喜欢
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        相关资源
        最近更新 更多