POI api Projects

POI api Project 1

Merging cells using POI
Previous Home Next


In this page of the example we are try to merge the two or more cell with each other in the excel sheet. for performing this action POI will help us. because it provide the special functionality to completion of this task.

 In this page of the example, firstly we create the object of the HSSFWorkbook() then create the sheet object in this workbook with createSheet("new sheet") method. after that we are creating the row under the sheet by createRow() method. then next step is cell creation in the row with createcell() method.

package r4r;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.*;

public class mergecell {
public mergecell(int i, int j, int k, int l) {
		// TODO Auto-generated constructor stub

public static void main(String []args)throws IOException

		HSSFWorkbook wkb=new HSSFWorkbook();
	    HSSFSheet sheet = wkb.createSheet("new sheet");

	    HSSFRow row = sheet.createRow((short) 1);
	    HSSFCell cell = row.createCell((short) 1);
	    cell.setCellValue("This is a test of merging");

	    sheet.addMergedRegion(new CellRangeAddress(
	            1, //first row (0-based)
	            1, //last row  (0-based)
	            1, //first column (0-based)
	            2  //last column  (0-based)

	    // Write the output to a file
	    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
	    System.out.println("Merging is done");


Previous Home Next