* using an {@link InputStream} has a higher memory footprint
* than using a {@link File}.</p>
* <p>Note that in order to properly release resources the
- * Workbook should be closed after use.
+ * Workbook should be closed after use. Note also that loading
+ * from an InputStream requires more memory than loading
+ * from a File, so prefer {@link #create(File)} where possible.
* @throws EncryptedDocumentException If the workbook given is password protected
*/
public static Workbook create(InputStream inp) throws IOException, InvalidFormatException, EncryptedDocumentException {
* using an {@link InputStream} has a higher memory footprint
* than using a {@link File}.</p>
* <p>Note that in order to properly release resources the
- * Workbook should be closed after use.
+ * Workbook should be closed after use. Note also that loading
+ * from an InputStream requires more memory than loading
+ * from a File, so prefer {@link #create(File)} where possible.
* @throws EncryptedDocumentException If the wrong password is given for a protected file
*/
public static Workbook create(InputStream inp, String password) throws IOException, InvalidFormatException, EncryptedDocumentException {
throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
- // TODO file+password
/**
* Creates the appropriate HSSFWorkbook / XSSFWorkbook from
* the given File, which must exist and be readable.
* @throws EncryptedDocumentException If the workbook given is password protected
*/
public static Workbook create(File file) throws IOException, InvalidFormatException, EncryptedDocumentException {
+ return create(file, null);
+ }
+ /**
+ * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
+ * the given File, which must exist and be readable, and
+ * may be password protected
+ * <p>Note that in order to properly release resources the
+ * Workbook should be closed after use.
+ * @throws EncryptedDocumentException If the wrong password is given for a protected file
+ */
+ public static Workbook create(File file, String password) throws IOException, InvalidFormatException, EncryptedDocumentException {
if (! file.exists()) {
throw new FileNotFoundException(file.toString());
}
try {
- @SuppressWarnings("resource")
NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
- return new HSSFWorkbook(fs.getRoot(), true);
+ return create(fs, password);
} catch(OfficeXmlFileException e) {
// opening as .xls failed => try opening as .xlsx
OPCPackage pkg = OPCPackage.open(file);
public void testCreateWithPasswordFromFile() throws Exception {
Workbook wb;
- // TODO
+ // Unprotected, no password given, opens normally
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls), null
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb.close();
+
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx), null
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+
+ // Unprotected, wrong password, opens normally
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls), "wrong"
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb.close();
+
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx), "wrong"
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+
+ // Protected, correct password, opens fine
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls_prot[0]), xls_prot[1]
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb.close();
+
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), xlsx_prot[1]
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+
+ // Protected, wrong password, throws Exception
+ try {
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls_prot[0]), "wrong"
+ );
+ fail("Shouldn't be able to open with the wrong password");
+ } catch (EncryptedDocumentException e) {}
+
+ try {
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), "wrong"
+ );
+ fail("Shouldn't be able to open with the wrong password");
+ } catch (EncryptedDocumentException e) {}
}
}