officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-mergecells-x28-devexpress-dot-spreadsheet-dot-cellrange-x29.md
Merges the specified cells.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
void MergeCells(
CellRange range
)
Sub MergeCells(
range As CellRange
)
| Name | Type | Description |
|---|---|---|
| range | CellRange |
A CellRange object that specifies a range of cells to be merged.
|
To access a cell obtained as a result of merging a cell range, use a cell reference for the top left cell of the original range or a reference for any range with this top left cell within the original range.
To unmerge cells, use the Worksheet.UnMergeCells method.
Another way to merge and unmerge a cell range is to use the range’s RangeExtensions.Merge and RangeExtensions.UnMerge extension methods, defined by the RangeExtensions class. These extension methods are accessible as methods of the CellRange object and called by using the instance method syntax.
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"]);
' Merge cells contained in the range.
worksheet.MergeCells(worksheet.Range("A1:C5"))
The following code snippets (auto-collected from DevExpress Examples) contain references to the MergeCells(CellRange) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/FormulaActions.cs#L96
CellRange dataRangeHeader = worksheet.Range["A1:C1"];
worksheet.MergeCells(dataRangeHeader);
dataRangeHeader.Value = "myRange:";
CellRange dataRangeHeader = worksheet.Range["A1:C1"];
worksheet.MergeCells(dataRangeHeader);
dataRangeHeader.Value = "myRange:";
CellRange dataRangeHeader = worksheet.Range["A1:C1"];
worksheet.MergeCells(dataRangeHeader);
dataRangeHeader.Value = "myRange:";
web-dashboard-custom-info-exported-excel/CS/WebDashboardForm.aspx.cs#L43
sheet.MergeCells(sheet.Range.FromLTRB(5, 0, 8, 0));
Formatting formatting = textCell.BeginUpdateFormatting();
Dim dataRangeHeader As CellRange = worksheet.Range("A1:C1")
worksheet.MergeCells(dataRangeHeader)
dataRangeHeader.Value = "myRange:"
winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/FormulaActions.vb#L85
Dim dataRangeHeader As CellRange = worksheet.Range("A1:C1")
worksheet.MergeCells(dataRangeHeader)
dataRangeHeader.Value = "myRange:"
Dim dataRangeHeader As CellRange = worksheet.Range("A1:C1")
worksheet.MergeCells(dataRangeHeader)
dataRangeHeader.Value = "myRange:"
winforms-dashboard-customize-exported-document/VB/CustomExportDocumentExample/Form1.vb#L52
sheet.MergeCells(sheet.Range.FromLTRB(5, 0, 8, 0))
Dim formatting As Formatting = textCell.BeginUpdateFormatting()
web-dashboard-custom-info-exported-excel/VB/WebDashboardForm.aspx.vb#L45
textCell.Value = "Custom Document Header"
sheet.MergeCells(sheet.Range.FromLTRB(5, 0, 8, 0))
Dim formatting As Formatting = textCell.BeginUpdateFormatting()
See Also