Apache POI - Excel写入 - 锁定单个单元格

时间:2022-08-09 20:25:48

I am using Apache POI to generate Exccel Templete which my clients could download, add values and upload back.

我正在使用Apache POI生成我的客户可以下载的Exccel Templete,添加值并上传回来。

I would like to set the cell values non editable so that the template headers could not be edited.

我想将单元格值设置为不可编辑,以便无法编辑模板标题。

I tried this code but it does not work,

我试过这段代码,但它不起作用,

    cell.getCellStyle().setLocked(true)

I also read that locking the excel sheet and then allowing the columns to setlocked(false) would work but I am not sure how many columns will be filled by client, so I want is all other columns t be edited except the one which I filled dynamically with Apache POI.

我还读到锁定excel表然后允许列setlocked(false)可以工作,但我不确定客户端将填充多少列,所以我想要编辑所有其他列,除了我填写的列动态地使用Apache POI。

I hope my query is clear to understand.

我希望我的查询清楚明白。

2 个解决方案

#1


2  

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. 

#2


0  

I don't recall how well this works -- for example, I think that the client can unprotect the sheet using the menu -- but you do need to protect the sheet via something like Sheet.protectSheet("") (no password, but nevertheless a protected sheet.)

我不记得这有多好用 - 例如,我认为客户端可以使用菜单取消对表单的保护 - 但你需要通过Sheet.protectSheet(“”)之类的东西来保护表单(没有密码,但仍然是一张受保护的床单。)

#1


2  

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. 

#2


0  

I don't recall how well this works -- for example, I think that the client can unprotect the sheet using the menu -- but you do need to protect the sheet via something like Sheet.protectSheet("") (no password, but nevertheless a protected sheet.)

我不记得这有多好用 - 例如,我认为客户端可以使用菜单取消对表单的保护 - 但你需要通过Sheet.protectSheet(“”)之类的东西来保护表单(没有密码,但仍然是一张受保护的床单。)