KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > examples > EventExample


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hssf.usermodel.examples;
19
20 import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
21 import org.apache.poi.hssf.eventusermodel.HSSFListener;
22 import org.apache.poi.hssf.eventusermodel.HSSFRequest;
23 import org.apache.poi.hssf.record.*;
24 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
25
26 import java.io.FileInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30 /**
31  * This example shows how to use the event API for reading a file.
32  */

33 public class EventExample
34         implements HSSFListener
35 {
36     private SSTRecord sstrec;
37
38     /**
39      * This method listens for incoming records and handles them as required.
40      * @param record The record that was found while reading.
41      */

42     public void processRecord(Record record)
43     {
44         switch (record.getSid())
45         {
46             // the BOFRecord can represent either the beginning of a sheet or the workbook
47
case BOFRecord.sid:
48                 BOFRecord bof = (BOFRecord) record;
49                 if (bof.getType() == bof.TYPE_WORKBOOK)
50                 {
51                     System.out.println("Encountered workbook");
52                     // assigned to the class level member
53
} else if (bof.getType() == bof.TYPE_WORKSHEET)
54                 {
55                     System.out.println("Encountered sheet reference");
56                 }
57                 break;
58             case BoundSheetRecord.sid:
59                 BoundSheetRecord bsr = (BoundSheetRecord) record;
60                 System.out.println("New sheet named: " + bsr.getSheetname());
61                 break;
62             case RowRecord.sid:
63                 RowRecord rowrec = (RowRecord) record;
64                 System.out.println("Row found, first column at "
65                         + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());
66                 break;
67             case NumberRecord.sid:
68                 NumberRecord numrec = (NumberRecord) record;
69                 System.out.println("Cell found with value " + numrec.getValue()
70                         + " at row " + numrec.getRow() + " and column " + numrec.getColumn());
71                 break;
72                 // SSTRecords store a array of unique strings used in Excel.
73
case SSTRecord.sid:
74                 sstrec = (SSTRecord) record;
75                 for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
76                 {
77                     System.out.println("String table value " + k + " = " + sstrec.getString(k));
78                 }
79                 break;
80             case LabelSSTRecord.sid:
81                 LabelSSTRecord lrec = (LabelSSTRecord) record;
82                 System.out.println("String cell found with value "
83                         + sstrec.getString(lrec.getSSTIndex()));
84                 break;
85         }
86     }
87
88     /**
89      * Read an excel file and spit out what we find.
90      *
91      * @param args Expect one argument that is the file to read.
92      * @throws IOException When there is an error processing the file.
93      */

94     public static void main(String JavaDoc[] args) throws IOException JavaDoc
95     {
96         // create a new file input stream with the input file specified
97
// at the command line
98
FileInputStream JavaDoc fin = new FileInputStream JavaDoc(args[0]);
99         // create a new org.apache.poi.poifs.filesystem.Filesystem
100
POIFSFileSystem poifs = new POIFSFileSystem(fin);
101         // get the Workbook (excel part) stream in a InputStream
102
InputStream JavaDoc din = poifs.createDocumentInputStream("Workbook");
103         // construct out HSSFRequest object
104
HSSFRequest req = new HSSFRequest();
105         // lazy listen for ALL records with the listener shown above
106
req.addListenerForAllRecords(new EventExample());
107         // create our event factory
108
HSSFEventFactory factory = new HSSFEventFactory();
109         // process our events based on the document input stream
110
factory.processEvents(req, din);
111         // once all the events are processed close our file input stream
112
fin.close();
113         // and our document input stream (don't want to leak these!)
114
din.close();
115         System.out.println("done.");
116     }
117 }
118
Popular Tags