Take the 2-minute tour ×

I am trying to read an excel sheet of data, I am able to get the data and read it using HSSFWorkbook Everything works well, I am able to get the data.

I have set a password for the workbook, manually in the excel sheet Protect Workbook to "ABCXYZ" and while opening the excel file it will ask me the password and only after the password in correct it lets me in to write or read the excel file.

My question is how to get this password "ABCXYZ" in the java program.

HSSFWorkbook wb = new HSSFWorkbook(inputStream);
wb.isWriteProtected();--> This returns false.
share|improve this question

2 Answers 2

    try {           

        FileInputStream fileInput = new FileInputStream("sample.xls");         
        BufferedInputStream bufferInput = new BufferedInputStream(fileInput);            
        POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput);            

        HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true);            
        HSSFSheet sheet = workbook.getSheetAt(0);           

        HSSFRow row = sheet.createRow(0);
        Cell cell = row.createCell(0);

        cell.setCellValue("Hello World"); 

        FileOutputStream fileOut = new FileOutputStream(fname);
        workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");

    } catch (Exception ex) {

share|improve this answer
There is only one template document(Excel) so for that document I have set a password, I have set a password for that document. So If other documents are uploaded I should be validating using the password. It's only possible if I can get the password for that template document. –  Venkat Aug 23 '13 at 18:04

As far as I know, you can use:

(in Excel 2003)

POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("yourexcelfile.xls")); 
HSSFWorkbook wb = new HSSFWorkbook(pfs);

(in Excel 2007)

POIFSFileSystem pfs = newPOIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("yourexcelfile.xlsx")); 
EncryptionInfo encInfo = new EncryptionInfo(pfs); 
Decryptor decryptor = new Decryptor(encInfo); 
XSSFWorkbook wb = new XSSFWorkbook(decryptor.getDataStream(pfs));

Hope it helps!

share|improve this answer
The official documentation on this is here and agrees with what you've written –  Gagravarr Aug 23 '13 at 17:41
@razcor Even I used the Biff8EncryptionKey for setting up the password, and using this getCurrentUserPassword() you can retrieve the password that you set. This is not my question I am setting up the password in the excel sheet itself. I need to get that password in the java. –  Venkat Aug 23 '13 at 17:48
@Gagravarr I have already set the password for the excel document. I need to get that password to the Java code. –  Venkat Aug 23 '13 at 17:52
You'll need to clarify exactly what you tried, what happens, and why that isn't what you expected –  Gagravarr Aug 23 '13 at 18:10
I have an excel sheet with some data in it, I need to import that data and store in DB. So I am using HSSFWorkbook & HSSFSheet to get that data. For that excel sheet I have set a password so I need to check weather a valid excel sheet is being uploaded. So I need to get the password for that excel what ever the user is uploaded. –  Venkat Aug 23 '13 at 18:52

Your Answer


