[英]How to lock only cell content in Excel sheet
I am working on exporting some data to Excel using Java POI library. 我正在使用Java POI库将某些数据导出到Excel。 Some of the cells in this data have to be read-only. 此数据中的某些单元格必须是只读的。 I make it by: 我做到了:
CellStyle lockedCell = workbook.createCellStyle();
lockedCell.setLocked(true);
cell.setCellStyle(lockedCell);
and after it: 然后:
workbook.getSheetAt(workbook.getActiveSheetIndex()).protectSheet("");
It works quite well, but there is one problem - I cannot modify the width of columns containing locked cells. 它工作得很好,但是有一个问题-我无法修改包含锁定单元格的列的宽度。
Is it possible to lock only the content of the cells, so that I could modify columns width? 是否可以仅锁定单元格的内容,以便修改列的宽度?
I do not think that it is possible to only allow user-width-modifications on locked cells. 我认为不可能仅在锁定的单元格上允许用户宽度修改。
What you can do instead is, after populating the data, call autoSizeColumn(colNumber);
您可以做的是,在填充数据后,调用autoSizeColumn(colNumber);
on the sheet for each column populated. 在工作表上填充的每一列。
int colMaxLength = workbook.getActiveSheetIndex().getRow(0).getLastCellNum();
HSSFSheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
for(int i = 0 ; i < colMaxLength ; i++){
activeSheet.autoSizeColumn(i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.