←Select platform

AddRegion Method

Summary

Adds a new merged cell region to the sheet based on the specified cell range. Facilitates the merging of multiple cells into a single cell region.

Syntax

C#
C++/CLI
Python
public int AddRegion( 
   CellRange range 
) 
public:  
   Int32 AddRegion( 
      CellRange^ range 
   ) 
def AddRegion(self,range): 

Parameters

range

A CellRange object specifying the start and end cells of the region to be merged. The range must encompass more than one cell.

Return Value

The index of the newly added merged cell region within the collection of merged regions. Returns -1 if the specified range is invalid or the merge operation fails.

Remarks

Merging cells is a powerful formatting feature in Excel, often used to create visually appealing headers, labels, or to group related data. However, it is important to use this feature judiciously to maintain the workbook's usability and integrity.

Example

C#
using Leadtools; 
using Leadtools.Document.LEADOffice.Sheet; 
 
 
public void MergeCellsAndSaveExample() 
{ 
   // Load an Excel workbook from a file 
   var workbook = LEADWorkbookFactory.Create(); 
 
   // Add a new sheet named "MergeExample" or access an existing sheet 
   var sheet = workbook.CreateSheet("MergeExample"); 
 
   // Define the cell range to merge (e.g., A1:B1) 
   var mergeRange = new CellRange(0, 0, 0, 1); // This assumes 0-based indexing for rows and columns 
 
   // Perform the merge operation on the specified cell range 
   sheet.MergedRegions.AddRegion(mergeRange); 
 
   // Optionally, set a value for the merged cells 
   var cell = sheet.CreateRow(0).CreateCell(0); // Access the first cell in the merge range 
   cell.SetCellValue("Merged Cell"); 
 
   // Save the workbook to disk 
   var filePath = Path.Combine(LEAD_VARS.ImagesDir, @"MergedCells.xlsx"); 
   workbook.Save(filePath); 
 
   Console.WriteLine($"Workbook saved with merged cells to {filePath}"); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 

Requirements

Target Platforms

See Also

MergedCellRegions Class

MergedCellRegions Members

Leadtools.Document.LEADOffice.Sheet Namespace

Help Version 23.0.2024.4.15
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document.LEADOffice Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.