The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Merge Cells or Split Merged Cells

  • 2 min to read
  • Merge Cells

    This example demonstrates how to merge several cells into a single cell. To do this, use the Worksheet.MergeCells method with the passed range of cells to be merged.

    Note

    When you merge a cell range, the data (value, formula and format settings) of only the top left non-empty cell will appear in the resulting merged cell. The data contained in other cells of the original range will be lost.

    If all cells within the original range are empty, the range's top left cell format will be applied to the merged cell.

    // Merge cells contained in the range.
    worksheet.MergeCells(worksheet.Range["A1:C5"]);
    
  • Split Merged Cells

    Call the Worksheet.UnMergeCells method with the passed range that contains merged cells to be split. Note that the content and formatting that were set in cells before merging will be lost (with the exception of the top left cell).

    
    // Split cells that were previously merged.
    worksheet.UnMergeCells(worksheet.Range["A1:C5"]);
    
  • Split All Merged Cells in a Worksheet

    To get all merged cells in the specified cell range, use the Range.GetMergedRanges method.

    
    // Split all merged cells in the worksheet.
    foreach (var item in worksheet.Cells.GetMergedRanges()) {
        item.UnMerge();
    }
    

The image below shows how cells are merged and split in a worksheet (the workbook is opened in Microsoft® Excel®). Note that only the "B2" data (the top left non-empty cell) appears in the merged cell.

Spreadsheet_Cell_MergeUnMerge