【问题标题】:How can we read protected password excel file (.xls) with POI API我们如何使用 POI API 读取受保护的密码 Excel 文件 (.xls)
【发布时间】:2009-07-30 03:53:23
【问题描述】:

我刚刚学习了 POI,发现 HSSF 非常易于阅读和创建 excel 文件 (.xls)。 但是,当我想读取受密码保护的 excel 时,我发现了一些问题。 我花了一个小时在互联网上找到这个解决方案。

请你帮我解决这个问题。 如果你能给我一个代码 sn-p,我很高兴。

谢谢。

【问题讨论】:

  • 您是否找到或没有找到解决此问题的方法?如果你这样做了,最好把它放在这里。

标签: apache-poi poi-hssf


【解决方案1】:

请参阅http://poi.apache.org/encryption.html - 如果您使用的是足够新的 Apache POI 副本(例如 3.8),则可以解密加密的 .xls 文件 (HSSF) 和 .xlsx 文件 (XSSF)(证明您有密码!)

目前您不能写出加密的 excel 文件,只能写出未加密的文件

【讨论】:

    【解决方案2】:

    在您撰写问题时,使用 Apache POI 并不容易。从那时起,支持已经走过了漫长的道路

    现在,如果你想打开一个密码保护的 Excel 文件,无论是.xls 还是.xlsx,只要你知道密码,你需要做的就是使用WorkbookFactory.create(File,Password),例如

    File input = new File("password-protected.xlsx");
    String password = "nice and secure";
    Workbook wb = WorkbookFactory.create(input, password);
    

    这将识别文件的类型,使用给定的密码对其进行解密,然后为您打开它。然后就可以正常阅读内容了

    【讨论】:

    • 传奇!我不知道有这么好的 API!
    【解决方案3】:

    这是一个完整的示例代码,它读取受保护的 excel 文件,使用密码解密并写出不受保护的 excel 文件

        public static void readProtectedBinFile() {
        try {
    
            InputStream inp = new FileInputStream("c:\\tmp\\protectedFile.xls");
            org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword("abracadabra"); 
    
            Workbook wb;
            wb = WorkbookFactory.create(inp);
    
            // Write the output to a file
            FileOutputStream fileOut;
            fileOut = new FileOutputStream("c:\\tmp\\unprotectedworkbook.xlsx");
            wb.write(fileOut);
            fileOut.close();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      这是读取 Excel 文件的代码,检查 .xls 和 .xlsx(有密码保护或无密码保护)作为完整示例代码。

       private Workbook createWorkbookByCheckExtension() throws IOException, InvalidFormatException {
                      Workbook workbook = null;
                      String filePath = "C:\\temp\\TestProtectedFile.xls";
                      String fileName = "TestProtectedFile.xls";
                      String fileExtensionName = fileName.substring(fileName.indexOf("."));
                      if (fileExtensionName.equals(".xls")) {                         
                          try {
                              FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                              workbook = new HSSFWorkbook(fileInputStream);                               
                          } catch (EncryptedDocumentException e) {
                              // Checking of .xls file with password protected.
                              FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                              Biff8EncryptionKey.setCurrentUserPassword("password");
                              workbook = new HSSFWorkbook(fileInputStream);
                          }                           
                      } else if (fileExtensionName.equals(".xlsx")){
                          // Checking of .xlsx file with password protected.
                          String isWorkbookLock = "";
                          InputStream is = null;
                          is = new FileInputStream(new File(filePath));
                          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);
                              try {
                                  d.verifyPassword("password");
                                  is = d.getDataStream(fs);
                                  workbook = new XSSFWorkbook(OPCPackage.open(is));
                                  isWorkbookLock = "true";
                              } catch (GeneralSecurityException e) {
                                  e.printStackTrace();
                              }               
                          }           
                          if (isWorkbookLock != "true") {
                              FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                              workbook = new XSSFWorkbook(fileInputStream);
                          }
                      }
                      return workbook;
                  }
      

      【讨论】:

      • 为什么不直接使用WorkbookFactory,它会为你做这一切?
      • 感谢您的建议。我们的项目使用了 poi-3.8-20120326.jar、poi-ooxml-3.8-20120326.jar 和 poi-ooxml-schemas-3.8-20120326.jar。似乎无法支持“Workbook wb = WorkbookFactory.create(File, String);”。它不适用于具有两个参数的 create() 方法,仅适用于一个参数。
      • 正如那些文件名中的日期巧妙地表明,这些罐子已有 3.5 年历史!如果你升级到较新的版本,你会发现上面提到的方法:)
      【解决方案5】:

      POI 将无法读取加密的工作簿 - 这意味着如果您保护了整个 工作簿(而不仅仅是一张工作表),那么它将不会能够阅读它。否则,它应该可以工作。

      【讨论】:

        【解决方案6】:

        拉维是对的。看来您可以读取受密码保护但不能使用 POI 加密的文件。见http://osdir.com/ml/user-poi.apache.org/2010-05/msg00118.html。下面的代码打印出文件的踪迹

        POIFSLister lister = new POIFSLister();
        lister.viewFile(spreadsheetPath, true);
        

        如果您得到一个提及加密的输出,那么您将无法使用 POI 打开文件。

        【讨论】:

          猜你喜欢
          • 2019-12-03
          • 1970-01-01
          • 2012-02-07
          • 2018-12-18
          • 2020-05-09
          • 2016-04-02
          • 1970-01-01
          • 2013-11-09
          • 1970-01-01
          相关资源
          最近更新 更多