Apache POI – Excel写入 – locking单个单元

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

我想设置单元格值不可编辑,以便模板标题不能被编辑。

我试过这个代码,但它不工作,

cell.getCellStyle().setLocked(true) 

我也读过,lockingExcel表,然后允许列locking(假)将工作,但我不知道有多less列将由客户端填写,所以我想要的是所有其他列被编辑,除了我填充dynamic使用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("") (无密码,但是却是一张受保护的纸张。)