Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 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

                         

» View all related tutorials
Related Tags: java c excel apache script import model setup io client method user print arguments type ip port sets anchor cli

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.