Home Jsp Poi Shifting Row Using JSP



Shifting Row Using JSP
Posted on: May 19, 2007 at 12:00 AM
In this program we are going to shift the row using java program.

Shifting row using JSP

     

In this program we are going to shift the row using java program.

Code description :

The packages we need to import are 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 and org.apache.poi.hssf.usermodel. HSSFWorkbook 
In this example, we first create 50 cells and then insert 50 values, then we shift rows up. The starting row is 5 and end row is 10 and which will be shifted up 5 rows. Don't confuse by starting  row 5 and end row 10. Because the row starts from 0 in excel file.

shiftRows(int startRow, int endRow, int n):

This method is used to shift the rows. There are three arguments passed in this method. The first one is startRow. This is passed to give the starting row and the endRow is second argument. This is passed for end row up to we have  shift. Third one is int " n" which is used to define the number of rows will be shift or we can say shifts rows between startRow and endRow n number of rows.
We can pass the third argument negative. The negative value mean shift rows up. 

 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 contentType="application/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%page import="java.util.*" %>
<%
try{
  HSSFWorkbook hwb = new HSSFWorkbook();
  HSSFSheet sheet = hwb.createSheet("new sheet");
  for(int i=0;i<50;i++)
  {
  HSSFRow row = sheet.createRow((short1+i);
  HSSFCell cell = row.createCell((short1);
  cell.setCellValue("rajesh");
  }
  sheet.shiftRows(510, -5);
  FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\shiftingRow.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.

Related Tags for Shifting Row Using JSP:
cexcelfileinsertvaluethiscelltarcreaterowstartifrowsexamplevaluesexamwssheilshiftlsusefromfirstartcecellsinstamcaesfuseenddoxawhichxampsstaratisirllmpleaandartsarconfrtrtsvassthstalupleplndonomo


More Tutorials from this section

Ask Questions?    Discuss: Shifting Row Using JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.