Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Merge and Split Cells

    • 3 minutes to read

    The DevExpress Spreadsheet API allows you to combine adjacent cells into one larger cell or restore a merged range to regular cells.

    Merge Cells

    Call the Worksheet.MergeCells method and pass the cell range to merge as a method parameter. The method merges all cells in the range and applies the first cell formatting to the result. The optional mode parameter accepts a MergeCellsMode value that specifies how to merge cells:

    Value Description
    BoundingRange Merges all cells in the range into a single cell. If the range overlaps existing merged ranges, the MergeCells method expands the range to include overlapping merged ranges.
    ByRows Merges cells in each row of the range. Each row becomes a separate merged cell.
    ByColumns Merges cells in each column of the range. Each column becomes a separate merged cell.

    If the target range intersects existing merged cells, the MergeCells method expands the target range to include all intersecting merged ranges.

    The top-left cell in the merged range preserves its value. The API clears values from the remaining cells in the merged range before it adds the range to Worksheet.MergedCells.

    The following code snippet merges A1:C1 into a single cell and keeps the text value in the first cell:

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Cells[0, 0].Value = "North Region";
    worksheet.MergeCells(worksheet.GetRange(0, 0, 1, 3));
    

    Split Merged Cells

    Call the Worksheet.UnmergeCells method to split a merged range back into individual cells. The UnmergeCells method removes every merged range that intersects the specified range.

    The following code snippet restores the merged cells in A1:C1 to independent cells:

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.MergeCells(worksheet.GetRange(0, 0, 1, 3));
    worksheet.UnmergeCells(worksheet.GetRange(0, 0, 1, 3));
    

    Inspect Merged Cells

    All merged ranges are rectangular regions stored in the worksheet’s MergedCells collection. Use the worksheet’s MergedCells collection to inspect current merged ranges:

    CellRange mergedRange = worksheet.MergedCells[0];
    CellRange foundRange = worksheet.MergedCells.GetMergedRange(0, 0);
    
    bool isMerged = worksheet.Cells[0, 0].IsMerged;
    

    Call the MergedCellCollection.GetMergedRange method to get the merged range that contains the specified cell. To get all merged ranges that intersect a cell range, call the CellRange.GetMergedRanges() method. The Cell.IsMerged property returns true when a cell belongs to a merged range.