使用apache poi将列设置为只读

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

And i am using **XSSFCellStyle** to achieve this but it's not working for me . 

整个代码是:

  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 is not get executed. styles.put("header", style5); 

在此先感谢,Simbu。

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

 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();