How to Create Excel Page Using JSP

In this program, we are going to create an excel using java .By going through the steps of this example we can create any number of excel pages.

How to Create Excel Page Using JSP

how to create excel page using jsp

     

In this program, we are going to create an excel using java .By going through the steps of  this example we can create any number of excel pages.

To make an excel page we can use third party POI APIs. This API is  provided by the Apache Jakarta (Jakarta POI - Java API To Access Microsoft Format Files ) Tomcat. You can download POI.jar form  Apache Jakarta Project it is open source.

The POI project consists APIs for manipulating  various file formats based on Microsoft, OLE 2 compound Document format using pure word files using Java. We can create, read or write MS Excel file using POI.
OLE 2 Compound Document Format is based on files including most Microsoft office files as XLS and DOC.

The package we need to import is java.io.InputStream and org.apache.poi.hssf.usermodel.HSSFWorkbook

The java.io.InputStream class is used to create file. We are creating an excel file named "rajesh.xls " The org.apache.poi.hssf.usermodel.HSSFWorkbook class is used to create an object of  HSSFWorkbook, by which we will write our rajesh.xls  file. This object allow us to modify the excel file.

HSSFWorkbook class extends java.lang.Object. HSSFWorkbook class is a high level representation of a workbook.You must create the object of HSSFBook to read and write a workbook. It is top most class for creating new excel sheets ,doc sheets etc. 

You have to follow the following step to execute this example: 
Steps : 

  1. Download the POI.jar from   Apache Jakarta Project .
  2. Extract it and then copy  poi-2.5.1-final-20040804.jar,poi-contrib-2.5.1-final-20040804.jar and poi-scratchpad-2.5.1-final-20040804.jar into C:\apache-tomcat-5.5.23\common\lib directory .
  3. Then download our example copy it into C:\apache-tomcat-5.5.23\webapps\excel.
  4. Start the web server. 
  5. Create folder into C drive with the name of C:\excel.  
  6. To compile and run open Internet Explore and write http://localhost:8080/excel/excelPoi.jsp
  7. After pressing enter or on click Go. Output "Your file has been created succesfully" will be diplay on browser 
  8. The excel file will be generated into C:\excel folder. 

The code of the program is given below:

<%page import="java.io.InputStream" %>
<%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%page import="java.io.*" %>
<%

  try {
  HSSFWorkbook wb = new HSSFWorkbook();
  FileOutputStream fileOut = new FileOutputStream
(
"C:\\excel\\rajesh.xls");
 wb.write(fileOut);
 fileOut.close();  
 out.println("Your file has been created succesfully");  
 catch Exception ex ) { 
  }  


%>

The output of the program is given below:

Download this example.