I need to unprotect a protected xlsx file.e.g Book1.xlsx Below code runs fine for the first time, Reads Book1.xlsx, decrypt it and again write it to the same filename.
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();
But when the same code tries to access the newly created unprotected Book1.xlsx(or anyother unprotected xlsx file) it fails and showing
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)
i need help in reading xlsx file and also unlock it using password, as done above.