使用POIFSFileSystem读取xlsx文件

Jav*_*ets 5 java apache-poi

我需要取消保护受保护的xlsx文件.eg Book1.xlsx下面的代码第一次运行正常,读取Book1.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();
Run Code Online (Sandbox Code Playgroud)

但是当相同的代码尝试访问新创建的不受保护的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)
Run Code Online (Sandbox Code Playgroud)

我需要帮助阅读xlsx文件,并使用密码解锁,如上所述.

小智 5

基本上,以下代码行不适用于Office 2007+ XML文档:

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
Run Code Online (Sandbox Code Playgroud)

因此,您首先需要检查输入流中的标头是否支持调用它:

POIFSFileSystem.hasPOIFSHeader(is)
Run Code Online (Sandbox Code Playgroud)

并且仅在以上返回true时才解密.该hasPOIFSHeader方法需要一个支持标记/重置的输入流,因此请检查它并将其包装在PushbackInputStreamif中.

把它们放在一起然后就变成这样:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)