Create and Save Excel File in JSP

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.

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 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, org.apache.poi.hssf.usermodel.HSSFSheet and org.apache.poi.hssf.usermodel. HSSFWorkbook.

The 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: 

  1. Download the .
  2. 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 .
  3. Then download our example copy it into your apache tomcat's "webapps" folder.
  4. Start the tomcat server. 
  5. To compile and run open Internet Explore and write http://localhost:8080/assign/excel.jsp. 
  6. Enter the name of file you want to give that xls file in the text box without .xls extention.
  7. After pressing enter or on click "Create" button . Output "Succesfully created file" will be display on success.jsp page. 
  8. The excel file will be generated into "c:\"

The code of the program is given below:

1. excel.jsp

<%@ page language="java" import="*" %>
<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" 

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="*" %>
<%String name=request.getParameter("filename");%>
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
row.createCell((short)2).setCellValue("amkit Kumar ");
FileOutputStream fileOut = new FileOutputStream("c:\\"+name+".xls");
}catch ( Exception ex ){ 

Successfully created file.

Output :




Download Source code

Share on Google+Share on Google+

Create and Save Excel File in JSP

Posted on: June 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Create and Save Excel File in JSP   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 31, 2011
Very Nice

Thank you so much for sharing.
April 8, 2011
save as type

HI, very helpful article but I have a question. How do I save an excel file created this way as a "Microsoft Excel 5.0/95 workbook" format. Thanks in advance.
sravan kumar
January 5, 2012
how to read cell style of excel file using poi

i have created excel file using poi-hssf and design cell with some style,i want to read that style like foreground color,back ground color etc..please give me code to read cell style asap..
September 26, 2012

Thanks so much for posting this example. If you have time, could be an excellent idea that you can teach how to save the file instantly whatever you wants. Instead of defining a single place. Thanks again!