Home Jsp Poi Create and Save Excel File in JSP
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Create and Save Excel File in JSP

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

  1. Download the poi-bin-3.1-beta2-20080526.zip(POI.jar .
  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="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

Download Source code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 19, 2008

Related Tutorials

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

Post your Comment


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

Thank you so much for sharing.
Muntazir
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..
Mixa
September 26, 2012
Thanks!!

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!
DMCA.com