Java: How to Merge or Unmerge Cells in Excel

 If you work with data in Microsoft Excel, you'll inevitably need to merge and unmerge cells. At the same time, if you want to create a header that spans columns or rows, you can merge Excel cells to do this easily in your spreadsheet. Merging cells combines two or more cells into a single cell, while unmerging cells splits a single cell into multiple cells. In this article, I will show you how to programmatically merge or unmerge cells in Excel. Read on for more information.

  • Merge Cells in Excel in Java
  • Unmerge Cells in Excel in Java

Programming Environment

Method 1:
Introduced manually. Download Free Spire.XLS for Java locally, unzip it, and find the Spire.Xls.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>

    <repository>

        <id>com.e-iceblue</id>

        <name>e-iceblue</name>

        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>

    </repository>

</repositories>

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>5.1.0</version>
    </dependency>

</dependencies>

Merge Cells in Excel in Java

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Get a specified range using Worksheet.getRange().get() method.
  • Merge cells in the specified range using XlsRange.merge() method.
  • Set the horizontal alignment of merged cells to Center using XlsRange.getCellStyle().setHorizontalAlignment() method.
  • Set the vertical alignment of merged cells to Center using XlsRange.getCellStyle().setVerticalAlignment() method.
  • Save the result document using Workbook.saveToFile() method.

Full Code

Java
import com.spire.xls.*;

public class
MergeCells {
    
public static void main(String[] args){

       
//Create a Workbook instance
       
Workbook workbook = new Workbook();

       
//Load a sample Excel document
       
workbook.loadFromFile("sample.xlsx");

       
//Get the first worksheet
       
Worksheet sheet = workbook.getWorksheets().get(0);

       
//Merge cells by range
       
sheet.getRange().get("A2:A4").merge();
       
sheet.getRange().get("A5:A7").merge();

       
//Set the horizontal alignment of merged cells to Center
       
sheet.getRange().get("A2").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
       
sheet.getRange().get("A5").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);

       
//Set the vertical alignment of merged cells to Center
        
sheet.getRange().get("A2").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
       
sheet.getRange().get("A5").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);


       
//Save the result document
       
workbook.saveToFile("MergeCells.xlsx", FileFormat.Version2013);
   
}
}
Effective Shot

Unmerge Cells in Excel in Java

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Get a specified range using Worksheet.getRange().get() method.
  • Unmerge cells in the specified range using XlsRange.unMerge() method.
  • Save the result document using Workbook.saveToFile() method.

Full Code

Java

import com.spire.xls.FileFormat;
import
com.spire.xls.Workbook;
import
com.spire.xls.Worksheet;

public class
UnmergeCells {
   
public static void main(String[] args){

       
//Create a Workbook instance
       
Workbook workbook = new Workbook();

       
//Load a sample Excel document
       
workbook.loadFromFile("MergeCells.xlsx");

       
//Get the first worksheet
       
Worksheet sheet = workbook.getWorksheets().get(0);

       
//Unmerge cells by range
       
sheet.getRange().get("A2:A4").unMerge();

       
//Save the result document
       
workbook.saveToFile("UnMergeCells.xlsx", FileFormat.Version2013);
   
}
}

Effective Shot

Conclusion:  

In this post, you have learned how to merge or unmerge cells in Excel in Java. Not only that, we also have other functions,

such as, Java: Insert or Delete Rows and Columns in ExcelJava: Create a Drop-Down List in Excel andso on. Apart from

that, if you'd like to learn more, you can visit this link to explore more about for Spire.XLS for Java.


Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application