簡體   English   中英

使用Apache POI獲取Excel工作簿密碼

[英]Get the excel workbook password using Apache POI

我正在嘗試讀取Excel數據表,我能夠獲取數據並使用HSSFWorkbook讀取數據。一切正常,我能夠獲取數據。

我已經為工作簿設置了密碼,在excel Protect WorkbookProtect Workbook手動將其設置為“ ABCXYZ”,並且在打開excel文件時會詢問我該密碼,並且只有在密碼正確后,它才允許我寫入或讀取excel文件。

我的問題是如何在Java程序中獲取此密碼“ ABCXYZ”。

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

據我所知,您可以使用:

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

希望能幫助到你!

    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) {



    } 

暫無
暫無

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

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