You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by DJP JEAN-PROST Dominique <Do...@sofaxis.com> on 2015/04/29 17:30:56 UTC
Create xlsx file with update protection
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
Re: Create xlsx file with update protection
Posted by Mark Beardsley <ma...@tiscali.co.uk>.
You would usually accomplish this by calling the setRevisionsPassword()
method which is defined on the XSSFWorkbook object. I have never worked with
encryption however so how you get from the OPCPackage to the XSSFWorkbook
preserving encryption is a bit of a mystery to me. It is possible to create
a new XSSFWorkbook by passing an OPCPackage object to it's constructor and
you might want to try this approach initially, something like the following;
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);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);
Then you could call the setRevisionPassword() method on the workbook, save
it away and try opening the file using Excel to see if that works for you.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-xlsx-file-with-update-protection-tp5718631p5718632.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org