Merge or Unmerge Range of Cells
Contents
[
Hide
]
You can use Aspose.Cells to merge or split a range of cells. Aspose.Cells provides the Range.merge() and Range.unMerge() methods for this purpose. This article explains how to merge a range of cells into a single cell.
The following sample code first creates a range - A1:D4 - and then merges the cells in the range into a single cell using the Range.merge() method. Similarly, it is possible to split cells by creating a range and calling the Range.unMerge() method.
The following image shows the output Excel file generated with the sample code. As you can see, the range A1:D4 has been merged into a single cell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(MergeUnmergeRangeofCells.class); | |
// Create a workbook | |
Workbook workbook = new Workbook(); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range | |
Range range = worksheet.getCells().createRange("A1:D4"); | |
// Merge range into a single cell | |
range.merge(); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |