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:
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.*;
|
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 |