【问题标题】:How to read password protected .xls file in java?如何在 java 中读取受密码保护的 .xls 文件?
【发布时间】:2015-01-02 11:05:09
【问题描述】:

我有一个代码在java 中读取受保护的 excel,但该代码给了我错误。 我的java代码。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class REadProtectedExcel {
    public static void main(String[] args) {
        String fname = "D:/Vijay/BRS_docs/10168/20.11.2014/20.11.2014/JCR_30.12.14_I Pay.xls";
        try {
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fname));
            EncryptionInfo info = new EncryptionInfo(fs);
            Decryptor d = Decryptor.getInstance(info);
            if (d.verifyPassword("vijay")) {
                System.out.println("Password correct");
                FileInputStream fis = new FileInputStream(fname);
                HSSFWorkbook workbook = new HSSFWorkbook(fis);
                HSSFSheet sheet = workbook.getSheetAt(0);
                Iterator rowIter = sheet.rowIterator();
                while (rowIter.hasNext()) {
                    HSSFRow myRow = (HSSFRow) rowIter.next();
                    Iterator cellIter = myRow.cellIterator();
                    while (cellIter.hasNext()) {
                        String cellvalue = "";
                        HSSFCell myCell = (HSSFCell) cellIter.next();
                        if (myCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            cellvalue = myCell.getStringCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            cellvalue = "" + myCell.getNumericCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                            cellvalue = "" + myCell.getBooleanCellValue();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                            cellvalue = "" + myCell.getCellFormula();
                        } else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                            cellvalue = "";
                        }
                        System.out.println("cellvalue--" + cellvalue);
                    }
                }
            } else {
                System.out.println("Password wrong");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
    }
}

这段代码给了我以下错误。

java.io.FileNotFoundException: no such entry: "EncryptionInfo"
    at org.apache.poi.poifs.filesystem.DirectoryNode.getEntry(DirectoryNode.java:375)
    at org.apache.poi.poifs.filesystem.DirectoryNode.createDocumentInputStream(DirectoryNode.java:177)
    at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:45)
    at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:39)
    at com.test.arrayList.REadProtectedExcel.main(REadProtectedExcel.java:22)
  • 我正在使用 poi3.9 jar,xmlbeans-2.3.0jar,poi-ooml-3.6
  • 我没有收到此问题。在此先感谢。
  • 或者请分享另一种读取 .xls 文件的方法。

【问题讨论】:

  • 你试过 xlsx 吗? xls 是硬性要求吗?
  • Password Protected Excel File 的可能重复项并寻找 poi 的第二个答案
  • 第二个答案不起作用。

标签: java apache-poi xls


【解决方案1】:

Apache POI provides documentation on Encryption on the website。如果您转到Apache POI homepage 并查看左侧菜单顶部附近,您会发现它链接在Encryption Support 下。我强烈建议您阅读它!

您将看到,您编写的代码用于加密的.xlsx 文件,它使用与旧的.xls 文件完全不同的方式来保护文件

正如文档所解释的,对于受密码保护的 .xls 文件,您需要做的只是:

String myPassword = "password";
org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(myPassword);
HSSFWorkbook wb = new HSSFWorkbook(stream);

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2016-04-02
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多