Currently, I am using Apache POI 3.16 with Java version 1.7.0-251 (Unix).
I found inspiration in an example provided by @Aniruddh Chandegra on how to create and edit a password-protected excel sheet using Apache POI 3.14 (Link here).
[EDIT - Below is the code snippet of what I'm doing: creating XSSFWorkbook, extracting data, and then encrypting it]
Note: The code is running on server-side Javascript with Mozilla Rhino v1.7R3, which supports ECMAScript Edition 5 and some features from Mozilla Javascript 1.8.
var wb = new XSSFWorkbook();
var createHelper = wb.getCreationHelper();
// Filling in rows/cells
addMostRecentSheet(wb);
var filepath = [hidden]
var fileOut = new java.io.FileOutputStream(filepath);
wb.write(fileOut);
fileOut.close();
var fs = new POIFSFileSystem();
var info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);
var enc = info.getEncryptor();
enc.confirmPassword("password");
var os = enc.getDataStream(fs);
opc.saveImpl(os);
opc.close();
var fos = new java.io.FileOutputStream(filepath);
fs.writeFilesystem(fos);
fos.close();
I managed to save a password-protected xlsx file but had to remove Date column styling.
Here's the code to format cells into the Date cell type:
function createDateCell(row, colNum, value)
{
var cell;
if (value)
{
cell = row.createCell(colNum);
cell.setCellValue(value);
var cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
cell.setCellStyle(cellStyle)
}
else
{
cell = row.createCell(colNum, Cell.CELL_TYPE_BLANK);
}
return cell;
}
However, when running the program, I consistently encounter this error. Is there a workaround to retain the Date column type?
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:
Fail to save: an error occurs while saving the package :
The part /xl/styles.xml failed to be saved in the stream with marshaller
org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@216fb8e