apache - How can I access password protected Excel workbook in Java using POI api -
I want to read and write with a password protected excel file. How can I use Apache POI API?
PoI should be able to open both protected xls files ( org.apache.poi Hssf.record.crypt ) and protected xlsx files (using org.apache.poi.poifs.crypt ) have you tried to do them?
If you are using HSSF (for an XLS file), then you have to set the password before opening the file. You do this with a call:
org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword (password);
After that, HSSF should be able to open your file.
For XSSF, you want something like this:
POIFSFileSystem fs = new POIFSFileSystem (new FileInputStream ("protect.xlsx")); EncryptionInfo Info = New Encryption Info (FS); Decryptter D = new decryptor (information); D.verifyPassword (Decryptor.DEFAULT_PASSWORD); XSSFWorkbook wb = New XSSFWorkbook (d.getDataStream (fs)); Alternatively, in newer versions of Apache POI, you can do something like this: workbook wb = WorkbookFactory.create (New file ("protected.xls"), "password"));
This will work for both HSSF and XSSF, depending on the format, selecting the correct one, and entering the given password in a proper way.
Comments
Post a Comment