Home Jsp Poi Reading And Writing Excel File



Reading And Writing Excel File
Posted on: May 19, 2007 at 12:00 AM
In this program, we are going to read and write the excel sheet using java .

reading and writing excel file

     

In this program, we are going to read and write the excel sheet  using java .

The package we need to import is :
java.io.*,
org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.poifs.filesystem.POISFileSystem,
org.apache.poi.hssf.usermodel. HSSFWorkbook


org.apache.poi.poif.filesystem.POIFileSystem class is used to buffer the excel file into an object. In other words, we can say that it is used to read the excel file. After reading the excel file we use write() method to write into any excel file. 

We can read any file and write it into other excel file by this way. You can add new cell and rows  or you can set any default values for any cells. This means you can read excel file and modify it according to your need.

To run this example you have to download the excel file and code first then make folder with the name of excel in C drive then copy the excel file and copy the code into webapps then follow the steps as defined in previous examples. 

The code of the program is given below:

<%page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%page import="org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
<%page contentType="application/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%
try{
 POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream
(
"c:\\excel\\rajesh1.xls"));
  HSSFWorkbook wb=new HSSFWorkbook(fs);
  FileOutputStream fileOut = new FileOutputStream(
"c:\\excel\\readingWriting.xls");
  wb.write(fileOut);
  fileOut.close();
  out.println("Your excel file has been generated");
  
  catch Exception ex ) { 
  }
%>

The output of the program is given below:

Download this excel file.

Download this example.

Related Tags for Reading And Writing Excel File:
cexcelwebfileiocopydownloadmakeloadwebappdefaultexamplesvinewvaluereadnamefolderintthiscellsetwriteapprowdefineifforrowsexampleaddwithvaluestoexamdrivemodifyrunldwseilitepsappslscanulfirstcecellsfaultinmodasmntpswebappscaddadesstepmemeanintomodippdodowndefinedxaxampseefoldanyoldkisirhallivmplprefolloweaandcopcodcodeccwebavassrirdthstepsavstapaluefaultfinpleplprndodeolonl


More Tutorials from this section

Ask Questions?    Discuss: Reading And Writing Excel File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.