Merging and Unmerging Cells
You don’t always want the same number of cells in every row or column. For example, you might want to put a title in a cell that spans several columns. Or, if creating an invoice, you might want fewer columns for the total. To make one cell from two or more cells, merge them. Microsoft Excel lets users select cells and merge them to structure the spreadsheet the way they want.
The result of merging and then splitting a range of cells formatted as the cells to the left in Microsoft Excel
Aspose.Cells supports this feature and can also merge cells in a worksheet. You may unmerge, or split, the merged cells too. A merged cell’s cell reference is the reference for the top-left cell in the originally selected range.
Note that when cells are merged, only the data in the top-left cell is retained. If there is data in the other cells in the range, that data is deleted.
Formatting, likewise, is based on the reference cell so that when you merge cells, the formatting settings of the top-left cell in the range are applied on the merged cell. When the cell is split, the new cells keep their original format settings.
Merging Cells in a Worksheet.
Using Microsoft Excel
The following steps describe how to merge cells in the worksheet using Microsoft Excel.
- Copy the data you want into the upper-leftmost cell within the range.
- Select the cells you want to merge.
- To merge cells in a row or column and center the cell contents, click Merge and Center icon on the Formatting toolbar.
Using Aspose.Cells
The Cells class has some useful methods for the task. For example, the method merge() merges the cells into a single cell within a specified range of the cells.
The following output is generated after executing the code below.
The cells (C6:E7) have been merged
Code Example
The following example shows how to merge cells (C6:E7) in a worksheet.
// 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.getSharedDataDir(MergingCellsInWorksheet.class) + "data/"; | |
// Create a Workbook. | |
Workbook wbk = new Workbook(); | |
// Create a Worksheet and get the first sheet. | |
Worksheet worksheet = wbk.getWorksheets().get(0); | |
// Create a Cells object to fetch all the cells. | |
Cells cells = worksheet.getCells(); | |
// Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.merge(5, 2, 2, 3); | |
// Input data into C6 Cell. | |
worksheet.getCells().get(5, 2).setValue("This is my value"); | |
// Create a Style object to fetch the Style of C6 Cell. | |
Style style = worksheet.getCells().get(5, 2).getStyle(); | |
// Create a Font object | |
Font font = style.getFont(); | |
// Set the name. | |
font.setName("Times New Roman"); | |
// Set the font size. | |
font.setSize(18); | |
// Set the font color | |
font.setColor(Color.getBlue()); | |
// Bold the text | |
font.setBold(true); | |
// Make it italic | |
font.setItalic(true); | |
// Set the backgrond color of C6 Cell to Red | |
style.setForegroundColor(Color.getRed()); | |
style.setPattern(BackgroundType.SOLID); | |
// Apply the Style to C6 Cell. | |
cells.get(5, 2).setStyle(style); | |
// Save the Workbook. | |
wbk.save(dataDir + "mergingcells_out.xls"); | |
wbk.save(dataDir + "mergingcells_out.xlsx"); | |
wbk.save(dataDir + "mergingcells_out.ods"); | |
// Print message | |
System.out.println("Process completed successfully"); |
Unmerging (Splitting) Merged Cells
Using Microsoft Excel
The following steps describe how to split merged cells using Microsoft Excel.
- Select the merged cell. When cells have been combined, Merge and Center is selected on the Formatting toolbar.
- Click Merge and Center on the Formatting toolbar.
Using Aspose.Cells
The Cells class has a method named unMerge() that splits cells into their original state. The method unmerges the cells using the cell’s reference in the merged cell range.
Code Example
The following example shows how to split the merged cells (C6). The example uses the file created in the previous example and splits the merged cells.
// 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.getSharedDataDir(UnMergingCellsInWorksheet.class) + "data/"; | |
// Create a Workbook. | |
Workbook wbk = new Workbook(dataDir + "mergingcells.xls"); | |
// Create a Worksheet and get the first sheet. | |
Worksheet worksheet = wbk.getWorksheets().get(0); | |
// Create a Cells object to fetch all the cells. | |
Cells cells = worksheet.getCells(); | |
// Unmerge the cells. | |
cells.unMerge(5, 2, 2, 3); | |
// Save the file. | |
wbk.save(dataDir + "UnMergingCellsInWorksheet_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |
Related Articles
- Finding and splitting merged cells.
- Merge and splitting a cell range using the Range.merge() and Range.unMerge() methods.