Apache POI - Excel Write - Lock Single Cell



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

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

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

    cell.getCellStyle().setLocked(true)

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

我希望我的查询清楚理解。

尝试以下代码,它可能会解决您的问题:

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("")的东西保护工作表(没有密码,但仍然是受保护的工作表。

最新更新