Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Poi Merging Two Cells

Related Tutorials


 
 

Share on Google+Share on Google+

Merging Two Cells

Advertisement
In this program we are going to merge two cells of an excel sheet through java program .

Merging two cells

     

In this program we are going to merge two cells of an excel sheet through java program .You can merge any number of cells.

The package we need to import is 
java.io.*,java.util.* ,
org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi. hssf.  usermodel. HSSFCellStyle, 
org.apache.poi.usermodel.HSSFRow, 
org.apache.poi.usermodel.HSSFCell
,
org.apache.poi.hssf.usermodel. HSSFWorkbook 
and
org.apache.poi.hssf.util.Region.

org.apache.poi.hssf.util.Region class is used to create an object of region. This class provides a method for adding the region. Region class is defined public and it extends the java.lang.Object .The Region class implements java.lang.Compareble and represents a form/to row/column square.

In this example, we are using following key points:

addMergedRegion(Region
r):
This method is used to merge the region which is passed as argument into this method.

In this example, we are merging the two cells into a single cell. In this example, Region(1,(short)1,1,(short)2) represents as Region(int rowFrom, short colFrom, int rowTo, short colTo). Here the rowFrom, colFrom, rowTo and colTo is used to define the region to merge. 

The code of the program is given below :

<%page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%page import="org.apache.poi.hssf.util.Region"%>
<%page contentType="application/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%page import="java.util.*" %>
<% try{
 HSSFWorkbook hwb = new HSSFWorkbook();
 HSSFSheet sheet = hwb.createSheet("new sheet");
 HSSFRow row = sheet.createRow((short1);
 HSSFCell cell = row.createCell((short1);
 cell.setCellValue("rajesh");
 sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
 FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\mergingExcel.xls");
 hwb.write(fileOut);
 fileOut.close();
   out.println("Your excel file has been generated");
 catch Exception ex ) {
 }
%>

The output of the program is given below:

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 19, 2007

Related Tutorials

Discuss: Merging Two Cells   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Lucinda
November 1, 2011
NlCtRJDzwQu

Glad I've finally found soemthing I agree with!
DMCA.com