In this paper, I will show you how to use Spire.XLS for Java Excel Java Merge & Unmerge Excel Cell
Tools used: Free Spire.XLS for Java Free Edition
Getting and installing Jar files:
Method 1: Download the jar file bag through the homepage. After downloading, unzip the file and put the Spire.xls.jar file under the lib folder into the Java program. Introduce to.
Method 2: maven warehouse Introduction by installation.
Merged Excel cell
In Spire.XLS, we can merge rows and columns of cells with the worksheet. getRange (). Get (). Merge () method.
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class MergeCells {
public static void main(String[] args){
//Workbook creation example
Workbook workbook = new Workbook();
//Import an Excel document
workbook.loadFromFile("Test1.xlsx");
//Get the first sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Combine cell ranges A 1 to C 1
sheet.getRange().get("A1:C1").merge();
//Save result document
workbook.saveToFile("MergeCells.xlsx", FileFormat.Version2013);
}
}
Unmerge cells
Spire.XLS Unmerge cells using the worksheet. GetRange (). Get (). UnMerge () method provided at the same time.
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class UnmergeCells {
public static void main(String[] args){
//Workbook creation example
Workbook workbook = new Workbook();
//Import an Excel document
workbook.loadFromFile("MergeCells.xlsx");
//Get the first sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Range of unmerging cells A 1 to C 1
sheet.getRange().get("A1:C1").unMerge();
//Save result document
workbook.saveToFile("UnMergeCells.xlsx", FileFormat.Version2013);
}
}
Recommended Posts