Hey guys I am working on a Swing application that logs data to a password protected excel sheet.
My initial problem is that I haven't been able to find proper documentation on how to create the excel sheet with password protection from scratch, and I am not entirely sure if it is even supported by Apache POI version 3.14. Any insight on the matter would be greatly appreciated.
However my real problem is that assuming I already have a password protected .xlsx file (by manually setting the password from within Excel itself), I was able to access the file through WorkbookFactory.create(new FileInputStream(dataFile), "password");
but once the code has executed, the file was no longer password protected and now anyone can access it.
Here's a snippet of the code I have:
// Sheet 1
private void logSheet1(File dataFile) throws IOException, InvalidFormatException {
Workbook workBook = WorkbookFactory.create(new FileInputStream(dataFile), "password");
Sheet sheet1 = workBook.getSheet("Sheet 1");
Row row = sheet1.createRow(sheet1.getLastRowNum()+1);
// data
for(int i=0; i<log.length; i++) {
if(log[i] == null) log[i] = new String("No data");
Cell cell = row.createCell(i);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(log[i]);
}
FileOutputStream fos = new FileOutputStream(dataFile);
workBook.write(fos);
fos.close();
}
// Sheet 2
private void logSheet2(File dataFile) throws IOException, InvalidFormatException {
Workbook workBook = WorkbookFactory.create(new FileInputStream(dataFile), "password");
Sheet sheet2 = workBook.getSheet("Sheet 2");
Row row = sheet2.createRow(sheet2.getLastRowNum()+1);
// data
for(int i=0; i<log.length; i++) {
if(log[i] == null) log[i] = new String("No data");
Cell cell = row.createCell(i);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(log[i]);
}
FileOutputStream fos = new FileOutputStream(dataFile);
workBook.write(fos);
fos.close();
}