Lock single column in Excel using Apache POI

前端 未结 1 711
别那么骄傲
别那么骄傲 2020-12-14 10:14

I want to create a Excel in which only a specific column is locked(Read-only), and the rest are editable,

I am using the following approach, but that doesn\'t seem t

1条回答
  •  抹茶落季
    2020-12-14 10:18

    If you do the opposite it works. Protect the whole sheet and call setLocked(false) for 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();
    

    0 讨论(0)
提交回复
热议问题