Hi all,

 

I want to lock certain cells inside an excel sheet. API says to use the
HSSFCellStyle.setLocked() method of as first step and the
HSSFSheet.protectSheet() to activate the lock settings.

So the simple code snippet I used for testing is:

 

//create workbook

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFSheet sheet = workbook.createSheet();

 

HSSFRow row1 = sheet.createRow(0);

HSSFCell cell1 = row1.createCell(0);

cell1.setCellValue(new HSSFRichTextString("cell 1"));

 

HSSFRow row2 = sheet.createRow(1);

HSSFCell cell2 = row2.createCell(0);

cell2.setCellValue(new HSSFRichTextString("cell 2"));

 

HSSFRow row3 = sheet.createRow(2);

HSSFCell cell3 = row3.createCell(0);

cell3.setCellValue(new HSSFRichTextString("cell 3"));

 

//lock test

cell1.getCellStyle().setLocked(true);

cell2.getCellStyle().setLocked(false);

cell3.getCellStyle().setLocked(true);

sheet.protectSheet("somePassword");

 

In the resulting sheet all cells are locked, even cell2. It turns out
that calling the setLocked() method on any style changes all other
styles simultaneously i.e. after calling cell3.setLocked(true) all three
styles change to state locked. The debugger tells me that the three
styles are different objects so there is no referencing issue at that
point.

 

Did anyone experience the same thing? I'm using the 3.2-FINAL.

 

Regards,

Christian

Reply via email to