POI HSSFCellStyle -> setLocked(boolean locked)

Status
Nicht offen für weitere Antworten.
R

Remo

Gast
Hi

habe eine Frage zur setLocked Methode von HSSFCellStyle von der POI-Library!
warum funktioniert es nicht, dass die Zelle gelockt ist, also gesperrt?

Code:
public class TestExcel {

	public static void main(String[] args) throws IOException {
		new TestExcel();
	}
	
	public TestExcel() throws IOException{
		File test = new File("test.xls");
		FileOutputStream fos = new FileOutputStream(test);
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("Test");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell((short)0);
		cell.setCellValue("Hallo");
		HSSFCellStyle style = workbook.createCellStyle();
		style.setLocked(true);
		cell.setCellStyle(style);
		workbook.write(fos);
	}
}

Danke für Hilfe

Gruss Remo
 
S

Sonia

Gast
The sheet must be set to protected in order for the setLocked method to have any effect. You need to set the protection at the sheet level first
Code:
HSSFSheet sheet = workbook.createSheet(sheetNames);
sheet.setProtect( true );

and then the setLocked() method works
Code:
HSSFCellStyle cs = workbook.createCellStyle();
cs.setLocked(true);

Note that if you want some cells to be locked and others to be editable, you need to explicitly set the setLocked property to false for all the editable fields.

Sonia
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben