Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Poi POI API Event

Related Tutorials


 
 

Share on Google+Share on Google+

POI API Event

Advertisement
In this program we are going to explain about POI API Event.

POI API Event 

     

In this program we are going to explain about POI 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 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 EventAPIsExample implements HSSFListener

public static void main(String[] argsthrows IOException
  
  
FileInputStream fin = new FileInputStream("example.xls");
  POIFSFileSystem poifs = new POIFSFileSystem(fin);
  InputStream din = poifs.createDocumentInputStream
(
"Workbook");
  HSSFRequest req = new HSSFRequest();
  req.addListenerForAllRecords(new EventAPIsExample());
  HSSFEventFactory factory = new HSSFEventFactory();
  System.out.println("\n\nExample of Event
 API using POI3.0)\n\n"
);
  factory.processEvents(req, din);
  fin.close();
  din.close();
  System.out.println("\n\nIn main method ->>
public static void main(String[] args)"
);
  }
  public void processRecord(Record record)
  {
  
 System.out.println("In Processs method ->>
public void processRecord(Record record)"
);
  }
}

The output of the program is given below:

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

C:\POI3.0\exmples\execl>java EventAPIsExample example.xls

Example of Event API using POI3.0)

In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)
In Processs method ->>public void processRecord(Record record)

In main method ->>public static void main(String[] args)

C:\POI3.0\exmples\execl>

 

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: March 14, 2008

Related Tutorials

Discuss: POI API Event   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com