Other API Samples

Live SpreadsheetGear API Samples

Range Samples Operations Merge Cells

Merge a range of cells

// Create a new workbook and some local variables.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;

// Get reference to range to merge.
SpreadsheetGear.IRange range = cells["B2:E5"];

// Merge the range.
range.Merge();

// Center horizontally and vertically.
range.HorizontalAlignment = SpreadsheetGear.HAlign.Center;
range.VerticalAlignment = SpreadsheetGear.VAlign.Center;

// Set the text of the merged cells.
range.Value = "Merged Cells";
Run This Sample
Render Image

Generate an image representation of the results of this sample, which uses the SpreadsheetGear.Drawing.Image class to convert ranges, charts and shapes to images.


Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.