001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel.examples;
019:
020: import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
021: import org.apache.poi.hssf.eventusermodel.HSSFListener;
022: import org.apache.poi.hssf.eventusermodel.HSSFRequest;
023: import org.apache.poi.hssf.record.*;
024: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
025:
026: import java.io.FileInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029:
030: /**
031: * This example shows how to use the event API for reading a file.
032: */
033: public class EventExample implements HSSFListener {
034: private SSTRecord sstrec;
035:
036: /**
037: * This method listens for incoming records and handles them as required.
038: * @param record The record that was found while reading.
039: */
040: public void processRecord(Record record) {
041: switch (record.getSid()) {
042: // the BOFRecord can represent either the beginning of a sheet or the workbook
043: case BOFRecord.sid:
044: BOFRecord bof = (BOFRecord) record;
045: if (bof.getType() == bof.TYPE_WORKBOOK) {
046: System.out.println("Encountered workbook");
047: // assigned to the class level member
048: } else if (bof.getType() == bof.TYPE_WORKSHEET) {
049: System.out.println("Encountered sheet reference");
050: }
051: break;
052: case BoundSheetRecord.sid:
053: BoundSheetRecord bsr = (BoundSheetRecord) record;
054: System.out
055: .println("New sheet named: " + bsr.getSheetname());
056: break;
057: case RowRecord.sid:
058: RowRecord rowrec = (RowRecord) record;
059: System.out.println("Row found, first column at "
060: + rowrec.getFirstCol() + " last column at "
061: + rowrec.getLastCol());
062: break;
063: case NumberRecord.sid:
064: NumberRecord numrec = (NumberRecord) record;
065: System.out.println("Cell found with value "
066: + numrec.getValue() + " at row " + numrec.getRow()
067: + " and column " + numrec.getColumn());
068: break;
069: // SSTRecords store a array of unique strings used in Excel.
070: case SSTRecord.sid:
071: sstrec = (SSTRecord) record;
072: for (int k = 0; k < sstrec.getNumUniqueStrings(); k++) {
073: System.out.println("String table value " + k + " = "
074: + sstrec.getString(k));
075: }
076: break;
077: case LabelSSTRecord.sid:
078: LabelSSTRecord lrec = (LabelSSTRecord) record;
079: System.out.println("String cell found with value "
080: + sstrec.getString(lrec.getSSTIndex()));
081: break;
082: }
083: }
084:
085: /**
086: * Read an excel file and spit out what we find.
087: *
088: * @param args Expect one argument that is the file to read.
089: * @throws IOException When there is an error processing the file.
090: */
091: public static void main(String[] args) throws IOException {
092: // create a new file input stream with the input file specified
093: // at the command line
094: FileInputStream fin = new FileInputStream(args[0]);
095: // create a new org.apache.poi.poifs.filesystem.Filesystem
096: POIFSFileSystem poifs = new POIFSFileSystem(fin);
097: // get the Workbook (excel part) stream in a InputStream
098: InputStream din = poifs.createDocumentInputStream("Workbook");
099: // construct out HSSFRequest object
100: HSSFRequest req = new HSSFRequest();
101: // lazy listen for ALL records with the listener shown above
102: req.addListenerForAllRecords(new EventExample());
103: // create our event factory
104: HSSFEventFactory factory = new HSSFEventFactory();
105: // process our events based on the document input stream
106: factory.processEvents(req, din);
107: // once all the events are processed close our file input stream
108: fin.close();
109: // and our document input stream (don't want to leak these!)
110: din.close();
111: System.out.println("done.");
112: }
113: }
|