我需要解除保護的xlsx文件.e.gl Book1.xlsx 下面的代碼首次運行正常,讀取Book1.xlsx,解密並再次將其寫入相同的文件名。使用POIFSFileSystem讀取xlsx文件
public static void unprotectXLSXSheet(String fileName, String password) {
try{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(password);
InputStream is = d.getDataStream(fs);
System.out.println(is.available());
XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(is));
FileOutputStream fileOut;
fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
但是當同樣的代碼試圖訪問新創建的未受保護的Book1.xlsx(或任何其它未受保護的xlsx檔案)失敗,並顯示
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
at com.wolseley.Excel.TestMainDummy.unprotectXLSXSheet(TestMainDummy.java:113)
at com.wolseley.Excel.TestMainDummy.main(TestMainDummy.java:52)
我需要幫助閱讀xlsx檔案也解開它使用密碼,如上所述。
感謝您的見解..,這確實的伎倆。 – JavaTweets 2014-11-04 09:20:31