EasyXLS™ library allows you to read merge areas or to export merge cells to Excel file.
Merging cells means combing two or more cells in one single cell. The text inside the first cell is spread over all the merged cells. The value from the merge area is kept in the cell from the left-upper corner of the area.
Source code sample
The below example shows how to export an Excel file with merge cells.
// Create an instance of the class that exports Excel files having one sheet
ExcelDocument workbook = new ExcelDocument(1);
// Get the table of data for the worksheet
ExcelTable xlsTable =
((ExcelWorksheet)workbook.easy_getSheet("Sheet1")).easy_getExcelTable();
// Merge cells
xlsTable.easy_mergeCells("A1:C3");
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Merge cells.xlsx");
' Create an instance of the class that exports Excel files having one sheetDim workbook As New ExcelDocument(1)
' Get the table of data for the worksheetDim xlsFirstTab As ExcelWorksheet = workbook.easy_getSheet("Sheet1")
Dim xlsTable = xlsFirstTab.easy_getExcelTable()
' Merge cells
xlsTable.easy_mergeCells("A1:C3")
' Export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Merge cells.xlsx")
C++// Create an instance of the class that exports Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Create one sheet
workbook->easy_addWorksheet_2("Sheet1");
// Get the table of data for the worksheet
EasyXLS::IExcelWorksheetPtr xlsFirstTab =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheet("Sheet1");
EasyXLS::IExcelTablePtr xlsTable = xlsFirstTab->easy_getExcelTable();
// Merge cells
xlsTable->easy_mergeCells_2("A1:C3");
// Export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Merge cells.xlsx");
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that exports Excel files having one sheet
ExcelDocument ^workbook = gcnew ExcelDocument(1);
// Get the table of data for the worksheet
ExcelWorksheet ^xlsFirstTab =
safe_cast<ExcelWorksheet^>(workbook->easy_getSheet("Sheet1"));
ExcelTable ^xlsTable = xlsFirstTab->easy_getExcelTable();
// Merge cells
xlsTable->easy_mergeCells("A1:C3");
// Export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Merge cells.xlsx");
Click here to see Continuous Code Listing
// Create an instance of the class that exports Excel files having one sheet
ExcelDocument workbook = new ExcelDocument(1);
// Get the table of data for the worksheet
ExcelTable xlsTable =
((ExcelWorksheet)workbook.easy_getSheet("Sheet1")).easy_getExcelTable();
// Merge cells
xlsTable.easy_mergeCells("A1:C3");
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Merge cells.xlsx");
.NET:// Create an instance of the class that exports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Create one sheet
$workbook->easy_addWorksheet_2("Sheet1");
// Get the table of data for the worksheet
$xlsTable = $workbook->easy_getSheet("Sheet1")->easy_getExcelTable();
// Merge cells
$xlsTable->easy_mergeCells_2("A1:C3");
// Export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Merge cells.xlsx");
Click here to see Continuous Code ListingJava:// Create an instance of the class that exports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Create a worksheet
$workbook->easy_addWorksheet("Sheet1");
// Get the table of data for the worksheet
$xlsTable = $workbook->easy_getSheet("Sheet1")->easy_getExcelTable();
// Merge cells by range
$xlsTable->easy_mergeCells("A1:C3");
// Export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Merge cells.xlsx");
Click here to see Continuous Code Listing
' Create an instance of the class that exports Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Create one sheet
workbook.easy_addWorksheet_2("Sheet1")
' Get the table of data for the worksheetset xlsTable = workbook.easy_getSheet("Sheet1").easy_getExcelTable()
' Merge cells
xlsTable.easy_mergeCells_2("A1:C3")
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Merge cells.xlsx")
' Create an instance of the class that exports Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Create one sheet
workbook.easy_addWorksheet_2 ("Sheet1")
' Get the table of data for the worksheetSet xlsTable = workbook.easy_getSheet("Sheet1").easy_getExcelTable()
' Merge cells
xlsTable.easy_mergeCells_2 ("A1:C3")
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Merge cells.xlsx")
' Create an instance of the class that exports Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Create one sheet
workbook.easy_addWorksheet_2("Sheet1")
' Get the table of data for the worksheetSet xlsTable = workbook.easy_getSheet("Sheet1").easy_getExcelTable()
' Merge cells
xlsTable.easy_mergeCells_2("A1:C3")
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Merge cells.xlsx")
<!-- Create an instance of the class that exports Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Create one sheet --><cfset ret = workbook.easy_addWorksheet("Sheet1")><!-- Get the table of data for the worksheet --><cfset xlsTable = workbook.easy_getSheet("Sheet1").easy_getExcelTable()><!-- Merge cells --><cfset xlsTable.easy_mergeCells("A1:C3")><!-- Export Excel file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Merge cells.xlsx")>
.NET:# Create an instance of the class that exports Excel files
workbook = ExcelDocument(1)
# Get the table of data for the worksheet
xlsTable = workbook.easy_getSheet("Sheet1").easy_getExcelTable()
# Merge cells by range
xlsTable.easy_mergeCells("A1:C3")
# Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Merge cells.xlsx")
Click here to see Continuous Code ListingJava:# Create an instance of the class that exports Excel files
workbook = gateway.jvm.ExcelDocument(1)
# Get the table of data for the worksheet
xlsTable = workbook.easy_getSheet("Sheet1").easy_getExcelTable()
# Merge cells by range
xlsTable.easy_mergeCells("A1:C3")
# Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Merge cells.xlsx")
Click here to see Continuous Code Listing
The screen shot below represents the exported Excel file with merged cells generated by the code sample above. The worksheet has merged cells from A1 to C3.