How to implement the POI Event
Previous | Home | Next |
In this page of the Example we are going to explain about POI API Event. The advantage of API event is that we can read an XLS with a relatively small memory. we know that for using to Event of POI we are constructing an instance of org.apache.poi.hssf.eventmodel.HSSFRequest. and for registering a class we have to create listenerorg.apache.poi.hssf.eventmodel.HSSFListener interface and useHSSFRequest.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 we can call HSSFRequest.addListenerForAllRecords(mylistener).
if we can register in listeners in the HSSFRequest object successfully we can construct an instance
oforg.apache.poi.poifs.filesystem.FileSystem and pass it our XLS file as inputstream. We can either pass this, along with the request we constructed, to an instance of HSSFEventFactory via theHSSFEventFactory.processWorkbookEvents(request, Filesystem) method, 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.
package r4r; 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 poieventtest implements HSSFListener{ public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("example.xls"); POIFSFileSystem pfsf = new POIFSFileSystem(fis); InputStream is = pfsf.createDocumentInputStream("Workbook"); HSSFRequest hreq = new HSSFRequest(); hreq.addListenerForAllRecords(new poieventtest()); HSSFEventFactory fact = new HSSFEventFactory(); System.out.println("Example of POI Event using POI2.5)\n"); fact.processEvents(hreq, is); fis.close(); is.close(); System.out.println("In main method\n"); } public void processRecord(Record record) { System.out.println("In Processs method"); } }
Previous | Home | Next |