보호 된 xlsx 파일을 보호 해제해야합니다 .e 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 (또는 anyother 보호되지 않은 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