微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 使用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();

原文地址:https://www.jb51.cc/java/125716.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐