I have existing xlsx spreadsheet. I am using Apache POI 3.17 to read it, add some entries and save as password protected spreadsheet in new file. After I run the program, new file is password protected, but I don't see new entries, only the ones which existed before. Here is simplified version of the program which opens empty spreadsheet, writes new cell and saves in new file with password. When I open file in Excel 2010 using password I still see empty spreadsheet. Any help will be appreciated. Thanks
public static void main(String[] args) {
try {
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("passw");
File is = new File("./empty.xlsx");
OPCPackage opc = OPCPackage.open(is, PackageAccess.READ_WRITE);
Workbook wb = WorkbookFactory.create(opc);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("CRYPT");
OutputStream encos = enc.getDataStream(fs);
opc.save(encos);
opc.close();
OutputStream fos = new FileOutputStream(new File("./f.xlsx"));
fs.writeFilesystem(fos);
fos.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
apache poiversion3.17while closingencos:Exception in thread "main" org.apache.poi.EncryptedDocumentException: java.io.FileNotFoundException: /tmp/poifiles/encrypted_package1071788535456667764crypt (...) at org.apache.poi.poifs.crypt.standard.StandardEncryptor$StandardCipherOutputStream.processPOIFSWriterEvent(StandardEncryptor.java:201) ... at org.apache.poi.poifs.crypt.standard.StandardEncryptor$StandardCipherOutputStream.close(StandardEncryptor.java:169)- Axel RichterFileinWorkbook wb = WorkbookFactory.create(new File("empty.xlsx"));, theempty.xlsxis updated too whilewb.writeupdates theOPCPackage. So after thatempty.xlsxcontains the new value inB2too and is not more "empty" ;-). As said in my answer, it lacks a possibility for programmmatically influencing the committing process between theXSSFWorkbookand it'sOPCPackage. - Axel Richter