0

I want to lock an Excel cell (none-editable cell) using Apache POI. I'm using HSSFWorkbook and here what I've tried so far.

First I created a style.

Font mystyle = workbook.createFont();
headerFont.setFontHeightInPoints((short) 11);
style = workbook.createCellStyle();
style.setLocked(true);

Then I added it like below.

resultCell.setCellValue("mystyle");
resultCell.setCellStyle((CellStyle) formatter.styles.get("mystyle"));

But it doesn't lock the cell, still I can edit the cell. Is there any other way to achieve this ?

RYJ
  • 439
  • 2
  • 16
  • 25
  • 2
    Please refer this discussion > https://stackoverflow.com/questions/13250095/how-to-make-cell-as-read-only-in-excel-using-apachi-poi – sulabh singh Feb 12 '18 at 07:40
  • @sulabhsingh I checked it also. It doesn't solve my problem. – RYJ Feb 12 '18 at 08:00
  • 1
    You need [protecting the Sheet](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Sheet.html#protectSheet-java.lang.String-) to cell locking taking effect. – Axel Richter Feb 12 '18 at 08:39
  • @AxelRichter I tried it too. I protected the sheet. But in this case whole sheet is protected and I can't edit any cell even I set it as `setLocked(false)` – RYJ Feb 12 '18 at 09:00
  • For me it works. And it is how Excel does it. All cells are locked by default according to their cell styles. You need setting the locking to false if you wants editing the cell while the sheet is protected. – Axel Richter Feb 12 '18 at 09:09

0 Answers0