当前位置:   article > 正文

java设置excel不可编辑不可复制_java操作excel,设置某些单元格不可编辑,xssfworkbook

java操作excel,设置某些单元格不可编辑,xssfworkbook
public static void createPointExcel(HttpServletRequest request, List<PointUtil> finalPointList) throws Exception {

	XSSFWorkbook wb = null;

	String excel = pathModel + "/point.xlsx";
	File fi = new File(excel);
	// 读取excel模板
	wb = new XSSFWorkbook(new FileInputStream(fi));
	// 读取了模板内所有sheet内容
	XSSFSheet sheet = wb.getSheetAt(0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

sheet.protectSheet(“password”); sheet.lockSelectLockedCells(true); sheet.lockSelectUnlockedCells(true); CellStyle lockedStyle = wb.createCellStyle(); lockedStyle.setLocked(true);

	// 在相应的单元格进行赋值
	int rowIndex = 1;
	// int j = 1;
	for (PointUtil pointUtil : finalPointList) {
		XSSFRow row = sheet.getRow(rowIndex);
		if (null == row) {
			row = sheet.createRow(rowIndex);
		}

		for (int i = 1; i < 6; i++) {
			XSSFCell cell = row.getCell(i);
			if (null == cell) {
				cell = row.createCell(i);
			}
			switch (i) {
			case 1:
			//不可编辑
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

cell.setCellStyle(lockedStyle);

				cell.setCellValue(pointUtil.getEQUIPMENT_CODE());
				break;
		
			}

		}

		rowIndex++;
	}

	// 输出Excel文件
	FileOutputStream output = new FileOutputStream(path + "/point" + System.nanoTime() + ".xlsx");
	wb.write(output);
	output.flush();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/170367
推荐阅读
相关标签
  

闽ICP备14008679号