使用 OpenXML 创建合并单元格

Create Merge Cells using OpenXML

请考虑这个Excel:

它是 XML:

我想使用 OpenXML 创建具有多个合并单元格的 Excel。

我该怎么做?

谢谢

您可以使用 MergeCells and MergeCell classes 来创建您需要的合并单元格。 MergeCells class 是合并单元格的集合(XML 中的 <mergeCells count="3">),MergeCell class 代表每组单独的合并单元格(<mergeCell ref="xx:xx" /> 在您的 XML 中)。要在合并的单元格中填充数据,您需要将值添加到最左上角的单元格;任何其他值都将被忽略。

以下代码将创建一个包含合并单元格的新文件。

using (SpreadsheetDocument myDoc = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
    WorkbookPart workbookpart = myDoc.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();

    SheetData sheetData = new SheetData();

    //add a row
    Row firstRow = new Row();
    firstRow.RowIndex = (UInt32)1;

    //create a cell in C1 (the upper left most cell of the merged cells)
    Cell dataCell = new Cell();
    dataCell.CellReference = "C1";
    CellValue cellValue = new CellValue();
    cellValue.Text = "99999";
    dataCell.Append(cellValue);

    firstRow.AppendChild(dataCell);

    sheetData.AppendChild(firstRow);
    // Add a WorkbookPart to the document.
    worksheetPart.Worksheet = new Worksheet(sheetData);

    //create a MergeCells class to hold each MergeCell
    MergeCells mergeCells = new MergeCells();

    //append a MergeCell to the mergeCells for each set of merged cells
    mergeCells.Append(new MergeCell() { Reference = new StringValue("C1:F1") });
    mergeCells.Append(new MergeCell() { Reference = new StringValue("A3:B3") });
    mergeCells.Append(new MergeCell() { Reference = new StringValue("G5:K5") });

    worksheetPart.Worksheet.InsertAfter(mergeCells, worksheetPart.Worksheet.Elements<SheetData>().First());

    //this is the part that was missing from your code
    Sheets sheets = myDoc.WorkbookPart.Workbook.AppendChild(new Sheets());
    sheets.AppendChild(new Sheet()
    {
        Id = myDoc.WorkbookPart.GetIdOfPart(myDoc.WorkbookPart.WorksheetParts.First()),
        SheetId = 1,
        Name = "Sheet1"
    });
}

上面的代码产生: