注:要锁定单元格需先为此表单设置保护密码,设置之后此表单默认为所有单元格锁定,可使用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.SheetAt(1);sheet.protectSheet(""); //设置表单保护密码 org.apache.poi.ss.usermodel.Row row = null;org.apache.poi.ss.usermodel.Cell cell = null;String cellValue = "132700002800";XSSFCellStyle alterableStyle = (ateCellStyle(); //获取当前单元格的样式对象alterableStyle.setLocked(true); //设定此单元格为锁定状态XSSFCellStyle nolockedStyle = (ateCellStyle(); //获取当前单元格的样式对象nolockedStyle.setLocked(false); //设定此单元格为非锁定状态 String value = "非锁定";for (int i = 0; i < 5; i++) {System.out.println(" i =" + i);row = ateRow(i);cell = ateCell(0);cell.setCellValue(cellValue);cell.setCellStyle(alterableStyle);cell = ateCell(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();}}
转载于:.html
本文发布于:2024-01-30 01:48:14,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170655049618368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |