8

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

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

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

    cell.getCellStyle().setLocked(true)

我还读到锁定excel表然后允许列设置锁定(假)会起作用,但我不确定客户端将填充多少列,所以我希望除了我填写的列之外的所有其他列都可以编辑使用 Apache POI 动态地。

我希望我的查询清楚易懂。

4

2 回答 2

3

试试下面的代码,它可能会解决你的问题:

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. 
于 2016-03-08T12:13:01.023 回答
0

我不记得这有多好——例如,我认为客户可以使用菜单取消保护工作表——但你确实需要通过类似的方式保护工作表Sheet.protectSheet("")(没有密码,但仍然是受保护的工作表。)

于 2013-09-13T02:55:20.523 回答