Password Protected Excel File

POI should be able to open both protected xls files (using org.apache.poi.hssf.record.crypt) and protected xlsx files (using org.apache.poi.poifs.crypt). Have you tried these?

If you’re using HSSF (for a xls file), you need to set the password before opening the file. You do this with a call to:

 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:

    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("protect.xlsx"));
    EncryptionInfo info = new EncryptionInfo(fs);
    Decryptor d = Decryptor.getInstance(info);
    d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
    XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs));

Full details are given on the POI Encryption documentation page

Leave a Comment