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.

Find Name of Excel Sheet

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:

 getSid():
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. 

getSheetname():
This method is used to find the name of sheet from  BoundSheetRecord.

 
 The code of the program is given below:
import java.io.*;
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.*;
import org.apache.poi.hssf.dev.EFHSSF;
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
(
"Workbook");
  HSSFRequest req = new HSSFRequest();
 req.addListenerForAllRecords(new FindNameOfSheet());
 HSSFEventFactory factory = new HSSFEventFactory();
  factory.processEvents(req, din);
  fin.close();
 din.close();
  System.out.println("STOP");
  }
  public void processRecord(Record record)
  {
  switch (record.getSid())  {
  
  case  BoundSheetRecord.sid:
  BoundSheetRecord bsrecord = 
(
BoundSheetRecordrecord;
 System.out.println("New sheet named: " +
 bsrecord.getSheetname
());
  break;
  } }  
  }

The output of the program is given below:

C:\POI3.0\exmples\execl>javac FindNameOfSheet.java

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

Download this example.