当前位置: 首页 > 知识库问答 >
问题:

使用POIFSFileSystem读取xlsx文件

汪翰墨
2023-03-14
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();
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文件,也使用密码解锁,正如上面所做的。

共有1个答案

徐飞龙
2023-03-14

下面的代码行基本上不适用于Office2007+XML文档:

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

因此首先需要检查输入流中的头是否通过调用以下命令得到支持:

POIFSFileSystem.hasPOIFSHeader(is)

并且只有在上面返回true时才解密。HaspoifSheader方法需要一个支持Mark/Reset的输入流,因此也要检查该输入流,如果不支持,则将其包装在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();
        }
    }
}
 类似资料:
  • 因此,我一直在使用Python3.2和OpenPyXL的iterable工作簿,如这里的“优化阅读器”示例所示。 当我尝试使用此策略读取从简单文档中提取的一个或多个文件时,就会出现问题。zip存档(手动和通过python zipfile包)。当我调用我得到“A”和我得到1,当要求打印每个单元格的值时,如下所示: 它打印A1、A2、A3、A4、A5、A6和A7中的值,而不管文件实际有多大。文件本身没

  • 我正在使用以下命令读取文件: 我在网上尝试了各种解决方案,它们告诉我要排除某些工件,例如:stax-api和stax。但所有的解决办法似乎都不奏效。

  • 问题内容: 我需要在Java应用程序中读取Excel 2007 XLSX文件。有谁知道一个很好的API来完成这项任务? 问题答案: AFAIK还没有可用的xlsx库。但是有些旧的xls: 一个库是jxls,它内部使用已经提到的POI。 其他2个链接:处理Excel文件,用于读写Excel XLS文档文件的Java库 。

  • 我在用图书馆 我在努力 库,但无法将其转换为工作簿 注意:在最终结果中,我希望返回XSSFWorkbook 上面的代码会内存溢出,任何帮助都将提前感谢

  • 问题内容: 我想使用python的Pandas库读取.xlsx文件,并将数据移植到postgreSQL表中。 到目前为止,我所能做的就是: 现在,我知道该步骤已成功执行,但是我想知道如何解析已读取的excel文件,以便可以了解excel中的数据如何映射到变量数据中的数据。 我没弄错,数据就是Dataframe对象。因此,我如何解析此dataframe对象以逐行提取每一行。 问题答案: 我通常会为每

  • 线程“main”org.apache.poi.xssf.usermodel.xssfactory.createdocumentPart(xssfactory.java:62)在org.apache.poi.poi.xssf.usermodel.xssfworkbook(xssfworkbook.java:271)在org.apache.poi.xssf.usermodel.xssfactory.c