Shifting Row Using JSP

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

Shifting Row Using JSP

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.