You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by "Weber, Christian" <Ch...@ids-scheer.com> on 2009/05/25 16:14:44 UTC

Locking cells

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


Re: Locking cells

Posted by Nick Burch <ni...@torchbox.com>.
On Mon, 25 May 2009, Weber, Christian wrote:
> //lock test
>
> cell1.getCellStyle().setLocked(true);
>
> cell2.getCellStyle().setLocked(false);
>
> cell3.getCellStyle().setLocked(true);

if cell 2 shares the same style as 1 or 3, this will not produce the 
effect you expect. Be aware that many cells can (and usually do) share the 
same style.

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Locking cells

Posted by Spike <so...@gmail.com>.
Sorry for the year and a half delay on the answer, but I've noticed that when
you call the protectSheet() method, all cells in the sheet will be locked
unless you tell them not to by setting a specific style.

But beware of sharing styles!! 





-- 
View this message in context: http://apache-poi.1045710.n5.nabble.com/Locking-cells-tp2307326p3250945.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org