使用POI设置导出的EXCEL锁定指定的单元格

注:要锁定单元格需先为此表单设置保护密码,设置之后此表单默认为所有单元格锁定,可使用setLocked(false)为指定单元格设置不锁定。

sheet.protectSheet("");//

    public static void WriteExcelByPoi(String fileData) throws IOException, 
                                                               InvalidFormatException {
        try {
            InputStream in = new FileInputStream(fileData);

            Workbook workbook = new XSSFWorkbook(in);
            org.apache.poi.ss.usermodel.Sheet sheet = (org.apache.poi.ss.usermodel.Sheet)workbook.getSheetAt(1);
            sheet.protectSheet(""); //设置表单保护密码


            org.apache.poi.ss.usermodel.Row row = null;
            org.apache.poi.ss.usermodel.Cell cell = null;

            String cellValue = "132700002800";
            XSSFCellStyle alterableStyle = (XSSFCellStyle)workbook.createCellStyle(); //获取当前单元格的样式对象
            alterableStyle.setLocked(true);    //设定此单元格为锁定状态
             XSSFCellStyle nolockedStyle = (XSSFCellStyle)workbook.createCellStyle(); //获取当前单元格的样式对象
             nolockedStyle.setLocked(false);    //设定此单元格为非锁定状态
             
             String value = "非锁定";

            for (int i = 0; i < 5; i++) {
                System.out.println(" i =" + i);
                row = sheet.createRow(i);
                cell = row.createCell(0);
                cell.setCellValue(cellValue);
                cell.setCellStyle(alterableStyle);
                cell = row.createCell(1);
                cell.setCellValue(value);
                cell.setCellStyle(nolockedStyle);
            }
            
            in.close();
            
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(fileData);
                workbook.write(out);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
原文地址:https://www.cnblogs.com/huanghongbo/p/4955772.html