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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
How to Create New Excel Sheet Using JSP 
 

In this program, we are going to create a new excel sheet using java .You can create any number of new excel sheets in a excel file.

 

How to create new excel sheet using jsp

                         

In this program, we are going to create a new  excel sheet using java .You can create any number of  new excel sheets in a excel file. 

To create a excel sheet we can use third party APIs .The name of this API is  POI. 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.

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, 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 "newrajesh.xls". The org.apache.poi.hssf.usermodel. HSSFWorkbook class is used to create an object of  HSSFWorkbook, by which we will write our new rajesh.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 is used to create a new sheet. This class extends java.lang.Object. It 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. As we are creating here six sheet as named  new sheet, second sheet ,third sheet ,fourth sheet, and fifth sheet.

You have to follow the following steps 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.HSSFSheet"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%page contentType="application/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%
        try {
           HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
   HSSFSheet sheet3 = wb.createSheet("third sheet");
    HSSFSheet sheet4 = wb.createSheet("fourth sheet");
   HSSFSheet sheet5 = wb.createSheet("third sheet");
    HSSFSheet sheet6 = wb.createSheet("fifth sheet");
    FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\newrajesh.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.

                         

» 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 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

i wont add data in exit xls sheet
how can i add ??

Posted by kalpesh patel on Friday, 02.22.08 @ 12:08pm | #49394

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.