2015-01-02 5 views
0

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(); 
     } 
    } 
} 

이 코드는 다음과 같은 오류를 표시합니다. 내가 poi3.9 항아리, XML 빈스 - 2.3.0jar을 사용하고

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) 
  • POI-ooml-3.6
  • 나는 사전에 issue.Thanks을 받고 있지 않다.
  • 또는 .xls 파일을 읽는 다른 방법을 공유하십시오.
+0

xlsx로 시도해 봤어? xls는 어려운 요구 사항입니까? –

+0

[암호로 보호 된 엑셀 파일] (http://stackoverflow.com/questions/2609301/password-protected-excel-file)의 가능한 복제본과 두 번째 응답이 작동하지 않는 po1 – Setu

+0

에 대한 두 번째 대답을 찾으십시오. – vijayk

답변

1

아파치 POI provides documentation on Encryption on the website. Apache POI homepage으로 이동하여 왼쪽의 메뉴 상단을 보면 Encryption Support에 링크되어있는 것을 볼 수 있습니다. 나는 당신이 그것을 읽을 것을 강력히 제안 할 것이다!

는 암호, 워드 프로세서로

설명 이전 .xls 파일에 파일을 보호하는 매우 다른 방법을 사용하여 암호화 된 .xlsx 파일에 대한 .xls 보호는 다음 보 겠지만, 당신이 작성한 코드입니다 파일을 만들려면 다음과 같이하십시오 :

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