java - XSSFSheet Apache POI - 是否可以锁定所有内容但允许更新和插入行?
问题描述
我尝试锁定整个工作表,但应该解锁某些列(我想在某些列中添加值)。我想复制一些行,添加新行并粘贴复制行中的值。可能吗?
public ByteArrayResource getQuestionnaireTemplate(List<QuestionnaireTemplateDto> questionnaireTemplateInitialData) throws IOException {
XSSFWorkbook workbook = excelExportService.createExcelWorkBook();
String frameworkName = questionnaireTemplateInitialData.stream().map(QuestionnaireTemplateDto::getFramework).findFirst().orElse("Framework Name");
XSSFSheet sheet = workbook.createSheet(frameworkName);
// sheet.lockInsertColumns(true);
// sheet.lockInsertRows(false);
sheet.enableLocking();
// sheet.lo
// CellStyle unlockedCellStyle = workbook.createCellStyle();
// unlockedCellStyle.setLocked(false);
CTSheetProtection sheetProtection = sheet.getCTWorksheet().getSheetProtection();
sheetProtection.setSelectLockedCells(false);
sheetProtection.setSelectUnlockedCells(false);
sheetProtection.setFormatCells(false);
sheetProtection.setFormatColumns(false);
sheetProtection.setFormatRows(false);
sheetProtection.setInsertColumns(false);
sheetProtection.setInsertRows(false);
sheetProtection.setInsertHyperlinks(false);
sheetProtection.setDeleteColumns(false);
sheetProtection.setDeleteRows(false);
sheetProtection.setSort(false);
sheetProtection.setAutoFilter(false);
sheetProtection.setPivotTables(false);
sheetProtection.setObjects(false);
sheetProtection.setScenarios(false);
然后对于某些行,我可以将某些单元格设置为可编辑(它有效):
private void addFieldRow(XSSFSheet sheet, XSSFCellStyle fieldRowStyle, QuestionnaireTemplateDto questionnaireTemplateDto) {
XSSFRow row = excelExportService.createRow(sheet,
sheet.getLastRowNum() + 1,
Arrays.asList(questionnaireTemplateDto.getFrameworkFieldId().toString(), questionnaireTemplateDto.getFramework(), questionnaireTemplateDto.getFieldName(), questionnaireTemplateDto.getYear().toString()),
fieldRowStyle);
CellStyle unlockedStyle = sheet.getWorkbook().createCellStyle();
unlockedStyle.setLocked(false);
XSSFCell cell = row.createCell(4);
cell.setCellStyle(unlockedStyle);
}
除了一个小细节外,我生成的工作表完美无缺-我无法插入新行并将其他行复制到其中...
我想我尝试了 Stack Overflow 的所有解决方案......
解决方案
您应该使用XSSFSheet方法而不是低级CTSheetProtection
方法来指定工作表保护。目前,使用- 方法apache poi 4.1.2
也可以使用所有可能性。XSSFSheet.lock...
但是你想要的并不是完全可能的。片材保护主要是保护细胞不发生变化。因此,如果您允许删除行并且该行包含受保护的单元格,则删除该行与单元格保护相矛盾。将一行复制到另一行也是如此。如果该另一行包含受保护的单元格,则复制也与单元格保护相矛盾。
以下完整示例创建一个工作簿,其中包含一个工作表,其中除列之外的所有单元格都受到保护A
,C
并且E
。工作表保护允许格式化行、插入行和删除行。但是删除行与该行中除列中的所有单元格的单元格保护相矛盾A
,C
并且E
。所以删除行是允许的,但不可能。
因此,生成的工作表只允许更改列中的单元格A
,C
和E
,格式化行(行高)和插入行。
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateExcelXSSFProtectedSheet {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
CellStyle notLocked = workbook.createCellStyle();
notLocked.setLocked(false);
Sheet sheet = workbook.createSheet();
sheet.setDefaultColumnStyle(0, notLocked); // column A is not locked
sheet.setDefaultColumnStyle(2, notLocked); // column C is not locked
sheet.setDefaultColumnStyle(4, notLocked); // column E is not locked
((XSSFSheet)sheet).lockFormatRows(false); // formatting rows is allowed
((XSSFSheet)sheet).lockInsertRows(false); // inserting rows is allowed
((XSSFSheet)sheet).lockDeleteRows(false); // deleting rows is allowed but may contradict cell protection
sheet.protectSheet("");
FileOutputStream out = new FileOutputStream("CreateExcelXSSFProtectedSheet.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
推荐阅读
- javascript - 自定义挂钩如何在引擎盖下工作?
- symlink - 我们可以在符号链接目录下创建文件吗?
- typescript - Vuex模块中的Typescript错误“当作为表达式调用时无法解析类装饰器的签名”
- javascript - 表单未提交,不确定这是否与验证码有关
- javascript - 如何用createElement渲染Vue的动态组件标签?
- snowflake-cloud-data-platform - 什么是磁盘溢出意味着以及如何在雪花中避免这种情况
- html - 如何删除所有默认锚样式以使元素不受影响?
- python - 冒泡排序算法和“int”对象不可下标
- r - 无法在 R 中安装 bbplot 包
- node.js - 如何将搜索结果发送回客户端?