获取使用Apache POI Excel工作簿密码(Get the excel workbook p

2019-10-29 20:27发布

我想读取数据的Excel工作表,我能够获得数据,并使用阅读HSSFWorkbook一切运作良好,我能够获得数据。

我已经为工作簿密码,手动在Excel工作表Protect Workbook ,以“ABCXYZ”,并在打开的Excel文件,它会问我的密码,只有在正确的密码后,它让我在写入或读取Excel文件。

我的问题是如何获得在Java程序中此密码“ABCXYZ”。

HSSFWorkbook wb = new HSSFWorkbook(inputStream);
wb.isWriteProtected();--> This returns false.

Answer 1:

据我所知,您可以使用:

(在Excel 2003中)

POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("yourexcelfile.xls")); 
Biff8EncryptionKey.setCurrentUserPassword("ABCXYZ"); 
HSSFWorkbook wb = new HSSFWorkbook(pfs);

(在Excel 2007中)

POIFSFileSystem pfs = newPOIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("yourexcelfile.xlsx")); 
EncryptionInfo encInfo = new EncryptionInfo(pfs); 
Decryptor decryptor = new Decryptor(encInfo); 
decryptor.verifyPassword("ABCXYZ"); 
XSSFWorkbook wb = new XSSFWorkbook(decryptor.getDataStream(pfs));

希望能帮助到你!



Answer 2:

    try {           

        FileInputStream fileInput = new FileInputStream("sample.xls");         
        BufferedInputStream bufferInput = new BufferedInputStream(fileInput);            
        POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput);            

        Biff8EncryptionKey.setCurrentUserPassword("password");            
        HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true);            
        HSSFSheet sheet = workbook.getSheetAt(0);           

        HSSFRow row = sheet.createRow(0);
        Cell cell = row.createCell(0);

        cell.setCellValue("Hello World"); 

        FileOutputStream fileOut = new FileOutputStream(fname);
        workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");
        workbook.write(fileOut);



    } catch (Exception ex) {



    } 


文章来源: Get the excel workbook password using Apache POI