Apache POI - Excel Write - Lock Single Cell

I use the Apache POI to generate an Exccel Templete that my clients could load, add values โ€‹โ€‹and load back.

I would like to set the cell values โ€‹โ€‹not editable so that the headings of the templates cannot be edited.

I tried this code but it does not work,

cell.getCellStyle().setLocked(true) 

I also read that locking an excel sheet and then allowing the columns to lock (false) will work, but I'm not sure how many columns will be populated by the client, so I want all the other columns to be edited except the one that I filled dynamically with Apache POI.

I hope my request is clear.

+8
java excel apache-poi
source share
2 answers

Try the following code, it may solve your problem:

 HSSFWorkbook workbook = new XSSFWorkbook(); // Cell styles. Note the setLocked(true) method call. HSSFCellStyle lockedNumericStyle = workbook.createCellStyle(); lockedNumericStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT); lockedNumericStyle.setLocked(true); HSSFSheet sheet = workbook.createSheet("Protection Test"); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue(100); cell.setCellStyle(lockedNumericStyle); // This line should cause all locked cells to be protected, // the user should not be able to change the cells // contents. sheet.protectSheet("password"); The password makes it possible to remove the protection from the sheet and makes it possible then for the locked cells to be modified. 
+1
source share

I donโ€™t remember how well this works - for example, I think that the client can remove protection from the sheet using the menu, but you need to protect the sheet through something like Sheet.protectSheet("") (there is no password, but less protected sheet.)

0
source share

All Articles