List of usage examples for org.apache.poi.poifs.filesystem DocumentFactoryHelper getDecryptedStream
public static InputStream getDecryptedStream(final DirectoryNode root, String password) throws IOException
From source file:com.jkoolcloud.tnt4j.streams.inputs.ExcelSXSSFRowStream.java
License:Apache License
/** * Reads HSSF (XLS) format excel file using Apache POI streaming SXSSF API. * * @param xlsFile// w ww.j av a 2s . c o m * excel HSSF format file to read * * @throws IOException * if excel file or workbook can't be read */ protected void readXLS(File xlsFile) throws IOException { NPOIFSFileSystem fs = null; InputStream dis = null; boolean passwordSet = false; try { fs = new NPOIFSFileSystem(xlsFile, true); DirectoryNode root = fs.getRoot(); if (root.hasEntry("EncryptedPackage")) { // NON-NLS dis = DocumentFactoryHelper.getDecryptedStream(fs, wbPass); } else { if (wbPass != null) { Biff8EncryptionKey.setCurrentUserPassword(wbPass); passwordSet = true; } dis = fs.createDocumentInputStream("Workbook"); // NON-NLS } HSSFRequest req = new HSSFRequest(); XLSEventListener listener = new XLSEventListener(this); FormatTrackingHSSFListener formatsListener = new FormatTrackingHSSFListener(listener, Locale.getDefault()); listener.setFormatListener(formatsListener); req.addListenerForAllRecords(formatsListener); HSSFEventFactory factory = new HSSFEventFactory(); factory.processEvents(req, dis); } finally { if (passwordSet) { Biff8EncryptionKey.setCurrentUserPassword((String) null); } Utils.close(fs); Utils.close(dis); } }