0

我正在尝试读取 Excel 数据表,我能够获取数据并使用HSSFWorkbook一切正常读取它,我能够获取数据。

我已经为工作簿设置了密码,在 Excel 表中手动设置Protect Workbook为“ABCXYZ”,在打开 excel 文件时它会询问我密码,只有在密码正确后,它才能让我写入或读取 excel 文件。

我的问题是如何在java程序中获取这个密码“ABCXYZ”。

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

2 回答 2

2

据我所知,您可以使用:

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

希望能帮助到你!

于 2013-08-23T16:52:20.927 回答
1
    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) {



    } 
于 2013-08-23T16:51:25.300 回答