Share on Google+Share on Google+

Find Name of Excel Sheet

In this program we are going to find the name of an excel sheet using POI3.0 API Event.

Find Name of Excel Sheet


In this program we are going to find the name of an excel sheet using POI3.0 API Event.

The advantage of
event API  is that you can read an XLS with a relatively small memory.

To use Event API we construct an instance of org.apache.poi.hssf.eventmodel.HSSFRequest. To register a class we have to create listener org.apache.poi.hssf.eventmodel.HSSFListener interface and use HSSFRequest.addListener(yourlistener, recordsid) method. The record Sid should be a static reference number (such as BOFRecord.sid) contained in the classes in org.apache.poi.hssf.record. Alternatively you can call HSSFRequest.addListenerForAllRecords(mylistener).

Once we have  registered our  listeners in the HSSFRequest object we can construct an instance of org.apache.poi.poifs.filesystem.FileSystem and pass it your XLS file inputstream. We can either pass this, along with the request we constructed, to an instance of HSSFEventFactory via the HSSFEventFactory.processWorkbookEvents(request, Filesystem) method, or we can get an instance of DocumentInputStream from Filesystem.createDocumentInputStream("Workbook") and pass it to HSSFEventFactory.processEvents(request, inputStream). Once we make this call, the listeners that we constructed receive calls to their processRecord(Record) methods with each Record they are registered to listen for until the file has been completely read.
The methods used in this example:

This method is used to give the description  copied  from class.  

public static final short sid:
The sid variable is used for less than operator as hex .

The public class BoundSheetRecord extends Record. This is used to defines a sheet within a workbook This is basically stores the sheet name and tells where the beginning of file record is within the HSSF file. 

This method is used to find the name of sheet from  BoundSheetRecord.

 The code of the program is given below:
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.eventusermodel.*;
import org.apache.poi.hssf.record.*;
public class FindNameOfSheet implements HSSFListener
  public static void main(String[] argsthrows IOException
 FileInputStream fin = new FileInputStream(args[0]);
 POIFSFileSystem poifs = new POIFSFileSystem(fin);
  InputStream din = poifs.createDocumentInputStream
  HSSFRequest req = new HSSFRequest();
 req.addListenerForAllRecords(new FindNameOfSheet());
 HSSFEventFactory factory = new HSSFEventFactory();
  factory.processEvents(req, din);
  public void processRecord(Record record)
  switch (record.getSid())  {
  case  BoundSheetRecord.sid:
  BoundSheetRecord bsrecord = 
 System.out.println("New sheet named: " +
  } }  

The output of the program is given below:


C:\POI3.0\exmples\execl>java FindNameOfSheet example.xls
New sheet named: Sheet1
New sheet named: Sheet2
New sheet named: Sheet3

Download this example.


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

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.

Discuss: Find Name of Excel Sheet   View All Comments

Post your Comment

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