简体   繁体   English

如何仅锁定Excel工作表中的单元格内容

[英]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.

相关问题 如何使用displaytag ExcelHssfView类在Excel工作表单元格中编写HTML内容 - How to write html content in excel sheet cell using displaytag ExcelHssfView class 如何在Excel工作表中垂直表示map&#39;content - how to represent map'content vertically in a excel sheet 如何使用 POI 锁定 Excel 工作表中的数据,使单元格没有任何数据/工作表的其余部分未锁定 - How to lock data in excel sheet using POI, leaving cells without any data / the rest of the sheet unlocked 如何在 Apache POI 中制作带有“逗号样式”单元格的 excel 工作表? - How to make excel sheet with "comma style" cell in Apache POI? 读取Excel工作表的空单元格 - Reading Empty Cell of Excel Sheet 如何从Excel工作表中的单元格中获取数值 - How to get the Numneric Value from a cell in an excel sheet 如何使用servlet在特定单元格的excel表中写入数据? - How to write data in excel sheet in specific cell using servlet? 如何使用 POI 将单元格注释添加到 Excel 工作表? - How to add Cell Comments to Excel sheet using POI? 如何使用JAVA获取Excel工作表中指定位置的单元格值 - How to get the value of a cell at a specified position in an excel sheet using JAVA 如何使用java为Excel工作表定义动态单元格范围? - How to define dynamic cell range for Excel sheet using java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM