2015-01-02 90 views
0

我有一个代码,读取java中的受保护的Excel,但该代码给我错误。 我的java代码。如何在java中读取密码保护的.xls文件?

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罐子,XMLBeans的-2.3.0jar,POI-ooml-3.6
  • 我没有事先得到issue.Thanks。
  • 或请分享阅读.xls文件的另一种方式。
+0

你用xlsx试过了吗? xls是一个很难的要求吗? –

+0

[密码保护的Excel文件](http://stackoverflow.com/questions/2609301/password-protected-excel-file)的可能的重复,并寻找poi – Setu

+0

的第二个答案,第二个答案不起作用。 – vijayk

回答

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); 
相关问题