1 DemoData(数据实体类)
package com.entity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class DemoData {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
/**
* 忽略这个字段
*/
@ExcelIgnore
private String ignore;
}
2 CommentWriteHandler(批注拦截器)
package com.handler;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 批注拦截器
*
*/
public class CommentWriteHandler extends AbstractRowWriteHandler {
/**
* 文档后缀名
*/
private String extension;
/**
* 列索引key
*/
public static final String COLINDEX_NAME = "colIndex";
/**
* 行索引key
*/
public static final String ROWINDEX_NAME = "rowIndex";
/**
* 批注内容key
*/
public static final String COMMENTCONTENT_NAME = "commentContent";
private boolean isFirst = true;
List<Map<String, String>> commentList = new ArrayList<>();
public CommentWriteHandler(List<Map<String, String>> commentList, String extension) {
this.commentList = commentList;
this.extension = extension;
}
@Override
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
Integer relativeRowIndex, Boolean isHead) {
if (isFirst == false) {
return;
}
isFirst = false;
Sheet sheet = writeSheetHolder.getSheet();
Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
if (commentList == null || commentList.size() <= 0) {
return;
}
for (Map<String, String> map : commentList) {
//批注内容
String commentContent = map.get(COMMENTCONTENT_NAME);
String colIndex = map.get(COLINDEX_NAME);
String rowIndex = map.get(ROWINDEX_NAME);
//该批注信息不完整
if (map.keySet().contains(COLINDEX_NAME) == false || StrUtil.isBlank(colIndex)
|| map.keySet().contains(ROWINDEX_NAME) == false || StrUtil.isBlank(map.get(ROWINDEX_NAME))
|| map.keySet().contains(COMMENTCONTENT_NAME) == false || StrUtil.isBlank(commentContent)) {
continue;
}
Row thisRow = sheet.getRow(Integer.parseInt(rowIndex));
if (thisRow == null) {
thisRow = sheet.createRow(Integer.parseInt(rowIndex));
}
Cell cell = sheet.getRow(Integer.parseInt(rowIndex)).getCell(Integer.parseInt(colIndex));
if (cell == null) {
cell = thisRow.createCell(Integer.parseInt(colIndex));
}
addComment(cell, commentContent, extension);
}
}
}
注:addComment()源码请参考以下博客。
3 调用代码
public void test() {
String fileName = "d:/commentWrite/commentWrite" + DateUtil.format(new Date(), "yyyy_MM_dd_HH_mm_ss") + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName, Map.class).inMemory(Boolean.TRUE).registerWriteHandler(new CommentWriteHandler(comment(), "xlsx")).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet);
// 千万别忘记finish 会帮忙关闭流
excelWriter.finish();
}
/**
* 生成测试批注
* @return
*/
public List<Map<String, String>> comment() {
List<Map<String, String>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
Map<String, String> map = new HashMap<>();
map.put(CommentWriteHandler.COLINDEX_NAME, j + "");
map.put(CommentWriteHandler.ROWINDEX_NAME, i + "");
map.put(CommentWriteHandler.COMMENTCONTENT_NAME, i + "*" + j);
list.add(map);
}
}
return list;
}
/**
* 生成测试数据
* @return
*/
public static List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}