Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

c# - Create Merge Cells using OpenXML

Please consider this Excel:

enter image description here

and it's XML:

enter image description here

I want to create such this Excel that has multiple merged cells using OpenXML.

How I can do this?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the MergeCells and MergeCell classes to create the merged cells you require. The MergeCells class is the collection of merge cells (<mergeCells count="3"> in your XML) and the MergeCell class represents each individual set of merged cells (<mergeCell ref="xx:xx" /> in your XML). To populate data in the merged cells you need to add the value to the upper left most cell; any other values will be ignored.

The following code will create a new file with merged cells.

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

The code above produces:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...