easyexcel 在 设置标题_七. EasyExcel标题加批注和标题字体填充红色
一, 概述
在日常开发中, 经常会碰到导入导出的场景, 有导入就肯定有导入模板, 本文将介绍利用EasyExcel给标题添加批注和挑剔字体填充颜色
二. 代码
2.1 编写样式处理类: TitleHandler
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import java.util.HashMap;
import java.util.List;
@Slf4j
public class TitleHandler implements CellWriteHandler {
//操作列
private List columnIndexs;
//颜色
private Short colorIndex;
// 批注
private HashMap annotationsMap;
public TitleHandler(List columnIndexs, Short colorIndex, HashMap annotationsMap) {
this.columnIndexs = columnIndexs;
this.colorIndex = colorIndex;
this.annotationsMap = annotationsMap;
}
public TitleHandler(List columnIndexs, Short colorIndex) {
this.columnIndexs = columnIndexs;
this.colorIndex = colorIndex;
}
@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) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if(isHead){
// 设置列宽
Sheet sheet = writeSheetHolder.getSheet();
sheet.setColumnWidth(cell.getColumnIndex(), 14 * 256);
writeSheetHolder.getSheet().getRow(0).setHeight((short)(1.8*256));
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
Drawing> drawing = sheet.createDrawingPatriarch();
// 设置标题字体样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("宋体");
headWriteFont.setFontHeightInPoints((short)14);
headWriteFont.setBold(true);
if (CollectionUtils.isNotEmpty(columnIndexs) &&
colorIndex != null &&
columnIndexs.contains(cell.getColumnIndex())) {
// 设置字体颜色
headWriteFont.setColor(colorIndex);
}
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
cell.setCellStyle(cellStyle);
if (null != annotationsMap && annotationsMap.containsKey(cell.getColumnIndex())) {
// 批注内容
String context = annotationsMap.get(cell.getColumnIndex());
// 创建绘图对象
Comment comment=drawing.createCellComment(new XSSFClientAnchor(0, 0, 0,0, (short) cell.getColumnIndex(), 0, (short) 5, 5));
comment.setString(new XSSFRichTextString(context));
cell.setCellComment(comment);
}
}
}
}
2.2. 编写工具类 : EasyExcelUtil
public class EasyExcelUtil {
/**
* 导出excel
* @param outputStream 输出流
* @param dataList 导出的数据
* @param classT 模板类
* @param sheetName sheetName
* @param cellWriteHandlers 样式处理类
*/
public static void writeExcelWithModel(OutputStream outputStream, List extends Object> dataList, Class extends Object> classT, String sheetName, CellWriteHandler... cellWriteHandlers) {
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 单元格策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 初始化表格样式
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
ExcelWriterSheetBuilder excelWriterSheetBuilder = EasyExcel.write(outputStream, classT).sheet(sheetName).registerWriteHandler(horizontalCellStyleStrategy);
if (null != cellWriteHandlers && cellWriteHandlers.length > 0) {
for (int i = 0; i < cellWriteHandlers.length; i++) {
excelWriterSheetBuilder.registerWriteHandler(cellWriteHandlers[i]);
}
}
// 开始导出
excelWriterSheetBuilder.doWrite(dataList);
}
}
2.3 测试
public class TestEasyExcel {
@Data
@ColumnWidth(20)
public static class TestVO {
@ExcelProperty(value = "*姓名", index = 0)
private String name;
@ExcelProperty(value = "*年龄", index = 1)
private int age;
@ExcelProperty(value = "学校", index = 2)
private String school;
}
/**
* 测试导出模板
* 1. 标题指定某列标红色字段
* 2. 标题指定某列加批注
*/
@Test
public void testExport1() throws FileNotFoundException {
// 输出流
OutputStream outputStream = new FileOutputStream(new File("D:\\1.xlsx"));
// 导出的数据
List dataList = new ArrayList<>();
// 指定标红色的列
List columns = Arrays.asList(0, 1);
// 指定批注
HashMap annotationsMap = new HashMap<>();
annotationsMap.put(0,"第一列标题批注");
annotationsMap.put(1,"第二列标题批注");
TitleHandler titleHandler = new TitleHandler(columns, IndexedColors.RED.index,annotationsMap);
EasyExcelUtil.writeExcelWithModel(outputStream, dataList, TestVO.class, "sheetName", titleHandler);
}
}
easyexcel 在 设置标题_七. EasyExcel标题加批注和标题字体填充红色相关推荐
- EasyExcel标题加批注和标题字体填充红色
七. EasyExcel标题加批注和标题字体填充红色 一, 概述 在日常开发中, 经常会碰到导入导出的场景, 有导入就肯定有导入模板, 本文将介绍利用EasyExcel给标题添加批注和挑剔字体填充颜色 ...
- 500个爆文标题_看了1000个爆文标题,终于发现了这个规律.......
文章要爆,标题要好,这不仅仅是一句口号而已! 爆文标题有何套路,什么样标题在自媒体领域更受欢迎?小易MM吭哧吭哧的从易撰爆文库搬来了1000个爆文标题数据做研究,今天就一起与大家来说说爆文标题的那些事 ...
- easyexcel 设置标题_使用easyexcel完成复杂表头及标题的导出功能(自定义样式)
如需客户端指定excel版本,只需要判断后缀名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定输出内容格式即可 ***(注意表格行高列宽统一设置是在实体类 ...
- python画图标题_使用pyplot.matshow()函数添加绘图标题
仅供参考 import matplotlib.pyplot as plt import numpy as np def samplemat(dims): """Make ...
- easyexcel 无模板写入_关于EasyExcel 的一些生成模板,导入导出的使用心得(优化版)...
这里就简单说了,直接贴代码 @HeadFontStyle(color =2) 维护表头第2行的注解 /** * @author: kuangql@fadada.com * @date: 2020/11 ...
- python xpath爬取新闻标题_爬取知乎热榜标题和连接 (python,requests,xpath)
用python爬取知乎的热榜,获取标题和链接. 环境和方法:ubantu16.04.python3.requests.xpath 1.用浏览器打开知乎,并登录 2.获取cookie和User-Agen ...
- go 设置进程名_七天用Go写个docker(第六天)
今天主要来实现一下 go-docker ps 的功能,也就是查看当前有哪些容器,简单说下思路,当我们启动一个容器时就为该容器创建一个文件夹用来保存该容器的一些信息,如果我们给容器指定了名字,那么该文件 ...
- 七牛 java 加水印_七牛云图片加水印
目标:用户登录进平台后,他看到的所有图片都要以他的用户名加上水印. 1.首先说下七牛加水印的方法,首先附上官网地址: https://developer.qiniu.com/dora/manual/1 ...
- iosxib 设置图片_修改xib 默认加载图片的路径
问题描述:项目组件化重构的时候,主工程的中xib 中引用了图片(图片是放在子工程中),但是不能显示. 问题原因:主工程Xib 加载的图片访问默认的是 main Bundle 中的资源,图片是子工程的F ...
最新文章
- 接口测试要如何做数据准备
- 赠票 | 深度强化学习的理论、算法与应用专题探索班
- java中怎样克隆,如何在Java中克隆列表?
- 基于Spring cloud Ribbon和Eureka实现客户端负载均衡
- Redis Lua脚本实现原子性操作
- linux ns,Linux Namespace (ns)
- 如何利用WGET覆写已存在的档案
- as3调用java_关于openamf我用as3链接java程序,并调用相关的方法,但是能够连上,却不能够调用是怎么回事...
- eclipse代码格式化
- unix服务器上传文件,Unix lrzsz命令 上传本地文件到服务器 / 发送文件到客户端...
- 企业选型数据库系统的五点建议
- 悦保OCR识别,提供票据、卡证、保险场景识别新方案
- dolphinscheduler 3.0.1 资源中心
- oracle delete加并发,并发delete导致oracle***锁问题的解决
- 识读第三角视图(机械识图)
- 读《Ivor Horton's beginning Visual C++ 2010》有感
- pytorch载入部分预训练权重
- 第一次用VNPY,通过仿真测试,踩过千万坑,我太难了~~~~~~
- TopCoder HowEasy
- UE4打包项目部署到Quest遇到的问题