Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 如何锁定XSSF工作簿中的特定单元格,以便用户无法编辑该单元格_Apache_Apache Poi_Xssf - Fatal编程技术网

Apache 如何锁定XSSF工作簿中的特定单元格,以便用户无法编辑该单元格

Apache 如何锁定XSSF工作簿中的特定单元格,以便用户无法编辑该单元格,apache,apache-poi,xssf,Apache,Apache Poi,Xssf,我正在使用ApachePOI读写XSSF工作簿 点击一个按钮,需要生成一个包含5张工作表的XSSF工作簿文件,.xlsm扩展名有50列,其中我填充了4列。。。。现在,一旦这个文件保存在用户系统中,我的代码填充的这4列数据应该是不可编辑的严格来说,只有这4列,其余的46列用户应该能够编辑数据。如何做到这一点???我可以使用最新的Apache POI您需要将单元格样式设置为锁定或解锁,并保护工作表。锁定工作表将默认所有单元格为只读 XSSFWorkbook workbook = new XSSFWo

我正在使用ApachePOI读写XSSF工作簿


点击一个按钮,需要生成一个包含5张工作表的XSSF工作簿文件,.xlsm扩展名有50列,其中我填充了4列。。。。现在,一旦这个文件保存在用户系统中,我的代码填充的这4列数据应该是不可编辑的严格来说,只有这4列,其余的46列用户应该能够编辑数据。如何做到这一点???我可以使用最新的Apache POI

您需要将单元格样式设置为锁定或解锁,并保护工作表。锁定工作表将默认所有单元格为只读

XSSFWorkbook workbook = new XSSFWorkbook(); 
XSSFSheet sheet = (XSSFSheet)workbook.createSheet("worksheet");
sheet.protectSheet("password");


CellStyle lockedStyle = workbook.createCellStyle();
lockedStyle.setLocked(true); 

CellStyle defaultStyle = workbook.createCellStyle();
defaultStyle.setLocked(false);

// Create a header row
Row row = sheet.createRow(0);    
Cell cell = row.createCell(0);
cell.setCellValue("locked");
cell.setCellStyle(lockedStyle);
sheet.autoSizeColumn(0);

cell = row.createCell(1);
cell.setCellValue("this cell unlocked");
cell.setCellStyle(defaultStyle);
sheet.autoSizeColumn(1);

cell = row.createCell(2);
cell.setCellValue("this column unlocked");
cell.setCellStyle(defaultStyle);
sheet.autoSizeColumn(2);

// sets any auto-generated cells in this column to unlocked
sheet.setDefaultColumnStyle(2, defaultStyle);

FileOutputStream fileOut =  new FileOutputStream("doc.xlsx");
workbook.write(fileOut);

workbook.close();
fileOut.close();