java - 使用 apache poi 将列设为只读

标签 java excel apache-poi

我正在使用 apache-poi 生成 excel 文件。我需要将第 4 列设置为只读,其余 2 列将由用户编辑。

我正在使用 XSSFCellStyle 来实现这一点,但它对我不起作用。

整个代码是:

Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();

XSSFCellStyle style5 = wb.createCellStyle();
XSSFFont headerFont = wb.createFont();
headerFont.setBold(true);
style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style5.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style5.setFont(headerFont);
style5.setLocked(true); // this line does not get executed.
styles.put("header", style5);

最佳答案

您必须保护整个工作表并解锁应该可编辑的单元格:

String file = "c:\\poitest.xlsx";
FileOutputStream outputStream = new FileOutputStream(file);
Workbook wb = new XSSFWorkbook();

CellStyle unlockedCellStyle = wb.createCellStyle();
unlockedCellStyle.setLocked(false);

Sheet sheet = wb.createSheet();
sheet.protectSheet("password");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
cell.setCellStyle(unlockedCellStyle);

wb.write(outputStream);
outputStream.close();

关于java - 使用 apache poi 将列设为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8502552/

相关文章:

java - 如何将 Set<List<Set<Integer>>> 传递给 hadoop map reduce 作业

java - Akka Actor 生命周期

java - 从 JBoss 7.1.1 迁移到 Wildfly 时的 @Context 问题

excel - 如何在excel或VBA中搜索具有多个 "-"的项目?

java - 检查 Apache POI 库中的单元格是否为空

java - 如果使用 jar,则从 jar 中解压文件,然后将提取的文件复制到目录中

php - Uncaught Error : Call to undefined function stats_cdf_t()

excel - VBA从excel写入文件标签

java - XSSFWorksheet 代码中未检测到空白

java - 如何在 Java 中更改 Apache POI 中的 XWPFTableCell 边距?