public static void WriteExcelByPoi(String fileData) throwsIOException,

InvalidFormatException {try{

InputStream in= newFileInputStream(fileData);

Workbook workbook= newXSSFWorkbook(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= newFileOutputStream(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();

}

}

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐