2014-11-04 2 views
4

보호 된 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 파일을 읽기에 도움을 필요로하고 또한 잠금을 해제 보여주는 그것은 위와 같이 암호를 사용합니다.

답변

5

기본적으로 다음 코드 줄 사무실 작동하지 않습니다 2007 이상 XML 문서 :

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName)); 

그래서 처음이를 호출하여이 지원 여부가 입력 스트림의 헤더를 확인해야합니다 :

POIFSFileSystem.hasPOIFSHeader(is) 

위의 내용이 참일 경우에만 해독하십시오. hasPOIFSHeader 메서드는 마크/리셋을 지원하는 입력 스트림이 필요하므로이를 확인하고 그렇지 않은 경우 PushbackInputStream에 래핑하십시오. 모두 함께 퍼팅

다음이 같은됩니다 : 당신의 통찰력을위한

public static void unprotectXLSXSheet(String fileName, String password) throws Exception { 
    InputStream is = null; 
    FileOutputStream fileOut = null; 

    try { 
     is = new FileInputStream(fileName); 
     if (!is.markSupported()) { 
      is = new PushbackInputStream(is, 8); 
     } 

     if (POIFSFileSystem.hasPOIFSHeader(is)) { 
      POIFSFileSystem fs = new POIFSFileSystem(is); 
      EncryptionInfo info = new EncryptionInfo(fs); 
      Decryptor d = Decryptor.getInstance(info); 
      d.verifyPassword(password); 
      is = d.getDataStream(fs); 
     } 

     System.out.println(is.available()); 
     XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(is)); 
     fileOut = new FileOutputStream(fileName); 
     wb.write(fileOut); 
     fileOut.flush(); 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
     if (fileOut != null) { 
      fileOut.close(); 
     } 
    } 
} 
+0

감사합니다 .. 정말 트릭을했다. – JavaTweets

0

아래에서 오래된 스택 오버플로 대답은 당신을 도울 수 있습니다. POIXMLProperties에서

OPCPackage pkg = OPCPackage.open(new File("file.xlsx")); 
POIXMLProperties props = new POIXMLProperties(pkg); 
System.out.println("The title is " + props.getCorePart().getTitle()); 

모든 속성에서 내장 및 사용자 지정 사람이 너무에 액세스 할 수 있습니다 :

Reading property sets from Office 2007+ documents with java poi

클래스 당신이 원하는거야 뭔가 같은 POIXMLProperties입니다!