Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 275
Arian Avatar asked May 12 '16 09:05

Arian


People also ask

How do I merge cells in Openxml?

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).

What is Documentformat Openxml?

The Open XML SDK provides tools for working with Office Word, Excel, and PowerPoint documents. It supports scenarios such as: - High-performance generation of word-processing documents, spreadsheets, and presentations. - Populating content in Word files from an XML data source.


1 Answers

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

like image 75
petelids Avatar answered Sep 22 '22 04:09

petelids