0

I have a code which reads a protected excel in java, but that code gives me error. My java code.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class REadProtectedExcel {
    public static void main(String[] args) {
        String fname = "D:/Vijay/BRS_docs/10168/20.11.2014/20.11.2014/JCR_30.12.14_I Pay.xls";
        try {
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fname));
            EncryptionInfo info = new EncryptionInfo(fs);
            Decryptor d = Decryptor.getInstance(info);
            if (d.verifyPassword("vijay")) {
                System.out.println("Password correct");
                FileInputStream fis = new FileInputStream(fname);
                HSSFWorkbook workbook = new HSSFWorkbook(fis);
                HSSFSheet sheet = workbook.getSheetAt(0);
                Iterator rowIter = sheet.rowIterator();
                while (rowIter.hasNext()) {
                    HSSFRow myRow = (HSSFRow) rowIter.next();
                    Iterator cellIter = myRow.cellIterator();
                    while (cellIter.hasNext()) {
                        String cellvalue = "";
                        HSSFCell myCell = (HSSFCell) cellIter.next();
                        if (myCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            cellvalue = myCell.getStringCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            cellvalue = "" + myCell.getNumericCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                            cellvalue = "" + myCell.getBooleanCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                            cellvalue = "" + myCell.getCellFormula();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                            cellvalue = "";
                        }
                        System.out.println("cellvalue--" + cellvalue);
                    }
                }
            } else {
                System.out.println("Password wrong");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
    }
}

This code gives me following error.

java.io.FileNotFoundException: no such entry: "EncryptionInfo"
    at org.apache.poi.poifs.filesystem.DirectoryNode.getEntry(DirectoryNode.java:375)
    at org.apache.poi.poifs.filesystem.DirectoryNode.createDocumentInputStream(DirectoryNode.java:177)
    at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:45)
    at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:39)
    at com.test.arrayList.REadProtectedExcel.main(REadProtectedExcel.java:22)
  • I am using poi3.9 jar,xmlbeans-2.3.0jar,poi-ooml-3.6
  • I am not getting the issue.Thanks in advance.
  • Or Please share another way to read the .xls file.
Jcl
  • 27,696
  • 5
  • 61
  • 92
vijayk
  • 2,633
  • 14
  • 38
  • 59

1 Answers1

1

Apache POI provides documentation on Encryption on the website. If you go to the Apache POI homepage and look near the top of the menu on the left, you'll find it linked under Encryption Support. I would strongly suggest you read it!

As you'll then see, the code you have written is for Encrypted .xlsx files, which use a very different way of protecting the file to the older .xls files

As the docs explain, for password protected .xls files, all you need to do is something like:

String myPassword = "password";
org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(myPassword);
HSSFWorkbook wb = new HSSFWorkbook(stream);
Gagravarr
  • 47,320
  • 10
  • 111
  • 156