excel 添加注释

1. 添加批注

获取指定行的指定列的单元格,给单元格添加批注

	public void setCellComment() {
		Row row = sheet.getRow(0);
		Iterator<Cell> iterator = row.iterator();
                // 遍历标题行,得到所有列的坐标及列名信息
		while (iterator.hasNext()) {
			Cell currentCell = iterator.next();
			// 列坐标,第几列
			int columnIndex = currentCell.getColumnIndex(); 
			// 列名
			String stringCellValue = currentCell.getStringCellValue();
		}
		Cell cell = row.getCell(0);
                Drawing patriarch = sheet.createDrawingPatriarch();
		cell.removeCellComment();
		ClientAnchor anchor = new XSSFClientAnchor();
		anchor.setDx1(0);
		anchor.setDx2(0);
		anchor.setDy1(0);
		anchor.setDy2(0);
		anchor.setCol1(cell.getColumnIndex());
		anchor.setRow1(cell.getRowIndex());
		anchor.setCol2(cell.getColumnIndex() + 2);
		anchor.setRow2(cell.getRowIndex() + 2);
		// 定义注释的大小和位置,详见文档
		Comment comment = patriarch.createCellComment(anchor);
		// 设置注释内容
		comment.setString(new XSSFRichTextString("这是测试批注"));
		// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
		comment.setAuthor("张三");
		cell.setCellComment(comment);
	}

2. 给批注单元格添加样式

	public static void setCellStyle(Workbook workbook, Cell cell) {
		CellStyle cellStyle = workbook.createCellStyle();
		//垂直居中
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		//水平居中
		cellStyle.setAlignment(HorizontalAlignment.CENTER);
                // 红色背景框
		cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                // 边框线条样式
		BorderStyle medium = BorderStyle.THIN;
                // 边框线条颜色
		short borderColor = IndexedColors.BLACK.getIndex();
                // 上下左右边框全部采用黑色实线
		cellStyle.setBorderBottom(medium);
		cellStyle.setBottomBorderColor(borderColor);
		cellStyle.setBorderLeft(medium);
		cellStyle.setLeftBorderColor(borderColor);
		cellStyle.setBorderRight(medium);
		cellStyle.setRightBorderColor(borderColor);
		cellStyle.setBorderTop(medium);
		cellStyle.setTopBorderColor(borderColor);
		cell.setCellStyle(cellStyle);
	}
如果文章对您有所帮助,可以点一下推荐哦
原文地址:https://www.cnblogs.com/virgosnail/p/15731650.html