Make column read-only using apache poi

后端 未结 3 1648
清歌不尽
清歌不尽 2020-12-03 12:28

I am using apache-poi for generating the excel file. I need to make the 4th column read-only and the remaining 2 columns will be editable by the user.

I am using

3条回答
  •  天命终不由人
    2020-12-03 13:08

    You have to protect the whole sheet and unlock the cells which should be editable:

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

提交回复
热议问题