简体   繁体   English

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

[英]Apache POI - Excel Write - Lock Single Cell

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. 希望我的查询清楚易懂。

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. 

我不记得这样做的效果如何-例如,我认为客户端可以使用菜单取消保护工作表-但您确实需要通过Sheet.protectSheet("")类的Sheet.protectSheet("")保护工作表(无需输入密码,但仍然是受保护的表。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM