• 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);
    	}
    
    如果文章对您有所帮助,可以点一下推荐哦
  • 相关阅读:
    CSDN社区之星专訪:我的蜕变之路
    Linux中运行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
    00075_BigInteger
    Delphi中Interface接口的使用方法
    Delphi结构体的扩展,可以自动初始化,反初始化,自定义拷贝函数.
    关于指针和堆栈
    Delphi 中的 procedure of object
    MikroTik RouterOS防火墙与过滤详解
    用 ROS 做内网DNS服务器
    GR32 TImage32的图层绘制原理
  • 原文地址:https://www.cnblogs.com/virgosnail/p/15731650.html
Copyright © 2020-2023  润新知