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



Home Java Poi Find Type of Work Book

Related Tutorials


 
 

Share on Google+Share on Google+

Find Type of Work Book

Advertisement
In this program we are going to find type of workbook using POI3.0 API Event.

Find Type of Work Book

     

In this program we are going to find type of  workbook using POI3.0 API Event.

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 the less than operator as hex .

The public class BOFRecord extends Record class. It is  used for the beginning of a set of records that have a particular purpose or subject. Used in sheets and workbooks

getType():
This method is used to find type of object that is marked. This method returns short type of object.

The different types of: workbook are:
1.TYPE_WORKBOOK,
2. TYPE_VB_MODULE
3.TYPE_WORKSHEET
4.TYPE_CHART
5.TYPE_EXCEL_4_MACRO,
6.TYPE_WORKSPACE_FILE
In this example we are finding the type of workbook and compare it BOFRecord  .If compression returns true the print we are printing a String message . 

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 TypeOfWorkBook 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 TypeOfWorkBook());
  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 BOFRecord.sid:
  BOFRecord bof = (BOFRecordrecord;
  if (bof.getType() == bof.TYPE_WORKBOOK)
  {
  System.out.println("Encountered workbook");
  
  
  break;
  }
 }
  
}

The output of the program is given below:

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

C:\POI3.0\exmples\execl>java TypeOfWorkBook example.xls
Encountered workbook
STOP

Download this example.

Advertisement

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: Find Type of Work Book   View All Comments

Post your Comment


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