POI reading password protected excel file
The ways to read Excel 2003(xls) and Excel 2007(xlsx) files are different .
1 2003(xls)
Please use the Biff8EncryptionKey Class to specify the password,and POI can automatically validate
whether the specied password is right. and open the excel file.
Example (code)
POIFSFileSystem pois = new POIFSFileSystem(new FileInputStream("test.xls"));
Biff8EncryptionKey.setCurrentUserPassword("password");
HSSFWorkbook book = new HSSFWorkbook(pois);
The method of setCurrentUserPassword is thread safe.
2 2007(xlsx)
The difference with 2007's way is that password validate is not automatical,
Example (code) -> the Junit test code from POI source package (DecryptorTest.java)
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = new Decryptor(info);
d.verifyPassword("password");
d.getDataStream(fs); -> inputStream
1 2003(xls)
Please use the Biff8EncryptionKey Class to specify the password,and POI can automatically validate
whether the specied password is right. and open the excel file.
Example (code)
POIFSFileSystem pois = new POIFSFileSystem(new FileInputStream("test.xls"));
Biff8EncryptionKey.setCurrentUserPassword("password");
HSSFWorkbook book = new HSSFWorkbook(pois);
The method of setCurrentUserPassword is thread safe.
2 2007(xlsx)
The difference with 2007's way is that password validate is not automatical,
Example (code) -> the Junit test code from POI source package (DecryptorTest.java)
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = new Decryptor(info);
d.verifyPassword("password");
d.getDataStream(fs); -> inputStream
package jp.ozero.poitest;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRichTextString;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.filesystem.POIFSFileSystem;public class Poitest {public static void main(final String[] args) throws Exception {// http://poi.apache.org/encryption.htmlString fname = "c:\\enc.xls";FileInputStream input = null;BufferedInputStream binput = null;POIFSFileSystem poifs = null;try {input = new FileInputStream(fname);binput = new BufferedInputStream(input);poifs = new POIFSFileSystem(binput);Biff8EncryptionKey.setCurrentUserPassword("MYPASSWORD");HSSFWorkbook workbook = new HSSFWorkbook(poifs);//HSSFSheet sheet = workbook.getSheetAt(0);int rows = sheet.getLastRowNum();System.out.println("rows:"+rows);for (int i = 0; i <= rows; i++) {HSSFRow row = sheet.getRow(i);if (row == null)continue;int cols = row.getLastCellNum();System.out.println("cols:"+cols);for (int j = 0; j <= cols; j++) {HSSFCell cell = row.getCell((int) j);if (cell == null)continue;if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {HSSFRichTextString val = cell.getRichStringCellValue();System.out.println(val);}if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {double val = cell.getNumericCellValue();System.out.println(val);}}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {binput.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("done");}}
留言
張貼留言