easyExcel设置单元格格式为文本,以及加批注

java 复制代码
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

public class ExcelCellWriteHandler implements CellWriteHandler {

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 2.0 设置单元格为文本
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat dataFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(dataFormat.getFormat("@"));
        cell.setCellStyle(cellStyle);
    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }
}
java 复制代码
/**
 * 自定义拦截器.新增注释,第一行头加批注
 *
 * @author Gsy
 */
@Slf4j
public class CommentWriteHandler implements RowWriteHandler {


	@Override
	public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {

	}

	@Override
	public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {

	}

	@Override
	public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead){
		if (!isHead){
			return;
		}
		Sheet sheet = writeSheetHolder.getSheet();
		Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
		// 将批注添加到单元格对象中
		Cell cell1 = sheet.getRow(0).getCell(0);
		Comment comment1 = ExcelUtil.addComments(drawingPatriarch, 0, 0, 0, 0, (short) 1, 0, (short) 2, 1, "长度限制20位");
		cell1.setCellComment(comment1);

		Cell cell2 = sheet.getRow(0).getCell(3);
		Comment comment2 = ExcelUtil.addComments(drawingPatriarch, 0, 0, 0, 0, (short) 4, 0, (short) 5, 1, "格式:男/女");
		cell2.setCellComment(comment2);

		Cell cell3 = sheet.getRow(0).getCell(4);
		Comment comment3 = ExcelUtil.addComments(drawingPatriarch, 0, 0, 0, 0, (short) 5, 0, (short) 6, 1, "示例:汉族");
		cell3.setCellComment(comment3);

		Cell cell4 = sheet.getRow(0).getCell(5);
		Comment comment4 = ExcelUtil.addComments(drawingPatriarch, 0, 0, 0, 0, (short) 6, 0, (short) 8, 1, "单元格格式:文本 格式:yyyy-MM-dd 示例:2023-01-02");
		cell4.setCellComment(comment4);

		Cell cell10 = sheet.getRow(0).getCell(7);
		Comment comment10 = ExcelUtil.addComments(drawingPatriarch, 0, 0, 0, 0, (short) 8, 0, (short) 9, 1, "长度限制11位");
		cell10.setCellComment(comment10);

		Cell cell5 = sheet.getRow(0).getCell(12);
		Comment comment5 = ExcelUtil.addComments(drawingPatriarch, 0, 0, 0, 0, (short) 13, 0, (short) 14, 1, "示例:一班");
		cell5.setCellComment(comment5);

		Cell cell6 = sheet.getRow(0).getCell(13);
		Comment comment6 = ExcelUtil.addComments(drawingPatriarch,0, 0, 0, 0, (short) 14, 0, (short) 15, 1, "示例:2012级 要求:入学年份+级");
		cell6.setCellComment(comment6);

		Cell cell7 = sheet.getRow(0).getCell(14);
		Comment comment7 = ExcelUtil.addComments(drawingPatriarch,0, 0, 0, 0, (short) 15, 0, (short) 16, 1, "要求:必须与系统中名称一致");
		cell7.setCellComment(comment7);

		Cell cell11 = sheet.getRow(0).getCell(16);
		Comment comment11 = ExcelUtil.addComments(drawingPatriarch,0, 0, 0, 0, (short) 17, 0, (short) 18, 1, "单位:cm");
		cell11.setCellComment(comment11);

		Cell cell12 = sheet.getRow(0).getCell(17);
		Comment comment12 = ExcelUtil.addComments(drawingPatriarch,0, 0, 0, 0, (short) 18, 0, (short) 19, 1, "单位:kg");
		cell12.setCellComment(comment12);

		Cell cell8 = sheet.getRow(0).getCell(42);
		Comment comment8 = ExcelUtil.addComments(drawingPatriarch,0, 0, 0, 0, (short) 43, 0, (short) 44, 1, "类型分为:不戴镜/框架眼镜/隐形眼镜/夜戴角膜塑形镜");
		cell8.setCellComment(comment8);


	}
}

用的时候在这里:

java 复制代码
前面代码省略
try {
			response.setContentType("application/vnd.ms-excel");
			String fileName = URLEncoder.encode("学生数据模板", "UTF-8");
			response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
			EasyExcel.write(response.getOutputStream(), StudentInfoExcel.class).inMemory(Boolean.TRUE).registerWriteHandler(new ExcelCellWriteHandler()).registerWriteHandler(new CommentWriteHandler()).sheet("学生数据表").doWrite(list);
		}catch (IOException e){
			e.printStackTrace();
		}
相关推荐
duration~33 分钟前
Maven随笔
java·maven
zmgst37 分钟前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
跃ZHD1 小时前
前后端分离,Jackson,Long精度丢失
java
blammmp1 小时前
Java:数据结构-枚举
java·开发语言·数据结构
暗黑起源喵1 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong2 小时前
Java反射
java·开发语言·反射
九圣残炎2 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge2 小时前
Netty篇(入门编程)
java·linux·服务器
Re.不晚3 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
雷神乐乐3 小时前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven