1 2 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 ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 30 33 public class EventExample 34 implements HSSFListener 35 { 36 private SSTRecord sstrec; 37 38 42 public void processRecord(Record record) 43 { 44 switch (record.getSid()) 45 { 46 case BOFRecord.sid: 48 BOFRecord bof = (BOFRecord) record; 49 if (bof.getType() == bof.TYPE_WORKBOOK) 50 { 51 System.out.println("Encountered workbook"); 52 } 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 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 94 public static void main(String [] args) throws IOException 95 { 96 FileInputStream fin = new FileInputStream (args[0]); 99 POIFSFileSystem poifs = new POIFSFileSystem(fin); 101 InputStream din = poifs.createDocumentInputStream("Workbook"); 103 HSSFRequest req = new HSSFRequest(); 105 req.addListenerForAllRecords(new EventExample()); 107 HSSFEventFactory factory = new HSSFEventFactory(); 109 factory.processEvents(req, din); 111 fin.close(); 113 din.close(); 115 System.out.println("done."); 116 } 117 } 118 | Popular Tags |