簡體   English   中英

如何在Java中讀取受密碼保護的.xls文件?

[英]How to read password protected .xls file in java?

我有一個代碼可以讀取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文件的方式。

Apache POI 在網站上提供有關加密的文檔 如果您轉到Apache POI主頁,然后在左側菜單的頂部附近查找,則會在“ 加密支持”下找到鏈接的鏈接。 我強烈建議您閱讀它!

如您所見,您編寫的代碼用於加密的.xlsx文件,該文件使用與舊版.xls文件保護文件的方式非常不同

正如文檔所解釋的那樣,對於受密碼保護的.xls文件,您所需要做的只是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM