Hello,

I’m trying to find a way of creating a xlsx file that would be password 
protected for update.
In Excel 2007, when saving a file with password protection, I’m asked to enter 
password for preventing file opening and another password for preventing file 
changes.
I managed to create the first use case with poi with this sample code, but it 
adds a password for the file to be opened, not for the file to be changed.
Is there a way I could do this ?

Thank you 
Dominique


        // create a new workbook
        final Workbook wb = new XSSFWorkbook();
…
// write the excel to a file
        try {
            final FileOutputStream fileOut = new 
FileOutputStream("c:/excel.xlsx");
            wb.write(fileOut);
            fileOut.close();
        } catch (final IOException e) {
            e.printStackTrace();
        }

        // Add password protection and encrypt the file

        final POIFSFileSystem poiFsFileSystem = new POIFSFileSystem();
        final EncryptionInfo encryptionInfo = new 
EncryptionInfo(poiFsFileSystem, EncryptionMode.agile);
        final Encryptor encryptor = encryptionInfo.getEncryptor();
        encryptor.confirmPassword("s3cr3t");
        final OPCPackage opcPackage = OPCPackage.open(new 
File("c:/excel.xlsx"), PackageAccess.READ_WRITE);
        final OutputStream os = encryptor.getDataStream(poiFsFileSystem);
        opcPackage.save(os);
        opcPackage.close();

        final FileOutputStream fos = new FileOutputStream("c:/excel.xlsx");
        poiFsFileSystem.writeFilesystem(fos);
        fos.close();

Consultez nos sites internet : 
http://www.sofaxis.com 
http://www.sofcap-sofcah.com

Sofaxis disclaimer : http://www.sofaxis.com/disclaimer-1.html

Reply via email to