@Override public void parse(Key key) throws IOException { _firstRow = true; InputStream is = DKV.get(key).openStream(); try { _fs = new POIFSFileSystem(is); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); _formatListener = new FormatTrackingHSSFListener(listener); HSSFEventFactory factory = new HSSFEventFactory(); HSSFRequest request = new HSSFRequest(); request.addListenerForAllRecords(_formatListener); factory.processWorkbookEvents(request, _fs); } finally { try { is.close(); } catch (IOException e) { } } }
/** * Processes a DocumentInputStream into essentially Record events. * * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String) * @param req an Instance of HSSFRequest which has your registered listeners * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object * @return numeric user-specified result code. */ private short genericProcessEvents(HSSFRequest req, InputStream in) throws HSSFUserException { short userCode = 0; // Create a new RecordStream and use that RecordFactoryInputStream recordStream = new RecordFactoryInputStream(in, false); // Process each record as they come in while (true) { Record r = recordStream.nextRecord(); if (r == null) { break; } userCode = req.processRecord(r); if (userCode != 0) { break; } } // All done, return our last code return userCode; }