Create and Save Excel File in JSP
Example program for creating and saving Excel file from JSP application.
In this example we are going to create a new excel sheet using JSP. Our application consists of two JSP files. Java program allows to you to create any number of excel sheets in an excel file.
To create an excel sheet we can use third party API which is Apache POI.
Apache POI is open source java library to access Microsoft file formats. This API is
available through apache website. You can download poi-bin-3.1-beta2-20080526.zip(POI.jar)
form Apache Jakarta Project.
In our jsp file we have to import the necessary packages to create the Excel
sheet from jsp page. The package we need to import is java.io.InputStream,
org.apache.poi.hssf.usermodel.HSSFSheet and org.apache.poi.hssf.usermodel.
HSSFWorkbook.
The java.io.InputStream class is used to create file. We are creating an
excel file named with ".xls" extension. The org.apache.poi.hssf.usermodel.
HSSFWorkbook class is used to create an object of HSSFWorkbook,
by which we will write our new ".xls" file. This object
allows us to modify the excel file. The org.apache.poi.hssf.usermodel.HSSFSheet
is used to create an object of sheet .
The org.apache.poi.hssf.usermodel.HSSFSheet class is used to create a new
sheet. This class is used to create a high level
worksheet.
To create new sheet, we use HSSFSheet constructor in which we pass the
name of the sheet as an string argument. We can create any number of
sheets and then add to the Excel file..
You have to follow the following steps to execute this example:
Steps:
- Download the poi-bin-3.1-beta2-20080526.zip(POI.jar) .
- Extract it and then copy poi-3.1-beta2-20080526.jar,poi-contrib-3.1-beta2-20080526.jar and poi-scratchpad-3.1-beta2-20080526.jar into your tomcat's lib directory .
- Then download our example copy it into your apache tomcat's "webapps" folder.
- Start the tomcat server.
- To compile and run open Internet Explore and write http://localhost:8080/assign/excel.jsp.
- Enter the name of file you want to give that xls file in the text box without .xls extention.
- After pressing enter or on click "Create" button . Output "Succesfully created file" will be display on success.jsp page.
- The excel file will be generated into "c:\".
The code of the program is given below:
1. excel.jsp
<%@ page language="java" import="java.io.*" %> <form method="post" action="success.jsp"> <p><font color="#800000" size="5">Enter XLS file name:</font> <input type="text" name="filename" size="20"></p> <p><input type="submit" value="Create" onclick="document.location='success.jsp';"/> </p> </form> |
2. success.jsp
<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%> <%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%> <%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%> <%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%> <%@ page import="java.io.*" %> <%String name=request.getParameter("filename");%> <%try{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("new sheet"); HSSFRow row = sheet.createRow((short)0); HSSFCell cell = row.createCell((short)0); cell.setCellValue(1); row.createCell((short)1).setCellValue(1.2); row.createCell((short)2).setCellValue("amkit Kumar "); row.createCell((short)3).setCellValue(true); FileOutputStream fileOut = new FileOutputStream("c:\\"+name+".xls"); wb.write(fileOut); fileOut.close(); }catch ( Exception ex ){ } %> Successfully created file. |
Output :
1.excel.jsp
2.success.jsp
3.amit.xls