EasyExcel 自定义单元格式的问题。
最近在做一个关于性能测试管理系统,一个新的需求,需要导出测试报告,直接使用了ali的封装的EasyExcel,但是在复杂头与一些样式,就缺少了自定义的灵活性,在官方demo中没有找到很好的解决方法。
不普及基础了,可以直接看官方文档, 直接上解决代码:
调用:
List<ApiStatusVO> apiStatusVOList = new ArrayList<>();
for(int i=0;i<=15;i++){
ApiStatusVO apiStatusVO = new ApiStatusVO();
apiStatusVO.setApiName("Apiname"+i);
apiStatusVO.setApiRt("RT"+i);
apiStatusVO.setApiStatus("200");
apiStatusVO.setId(i);
apiStatusVO.setTransId(String.valueOf(100+i));
apiStatusVO.setResponseAssert("true");
apiStatusVOList.add(apiStatusVO);
}
String fileName = URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd-24h").format(new Date())+".xlsx", "UTF-8");
ExcelKit().parseExcelAndSave(apiStatusVOList,fileName );
ExcelKit代码:
package com.kit;
import ……public class ExcelKit {
public static void parseExcelAndSave(List<ApiStatusVO> apiStatusVOS,String fileName){
EasyExcel.write(fileName, TestReportVO.class)
.head(head("我是一个标题头","描述:本次压测针对UXX常用接口进行压力测试;"))
.registerWriteHandler(new CustomCellWriteHandler())
.sheet("TestReport")
.doWrite(data(apiStatusVOS));
}
/** * Api 返回内容 数据不全,先自定义赋值
* @param statusVOS
* @return
*/
private static List<TestReportVO> data(List<ApiStatusVO> statusVOS){
List<TestReportVO> dataList = new ArrayList<>();
statusVOS.forEach(ApiStatusVO->{
TestReportVO status = new TestReportVO();
// @ExcelProperty("压测接口名称")
status.setTestName(ApiStatusVO.getApiName());
// @ExcelProperty("压测时间")
status.setTestDate(new Date());
// @ExcelProperty("压测时常(s)")
status.setTestDuration("100");
// @ExcelProperty("平均吞吐量(s)")
status.setTestHandlingCapacity("101");
// @ExcelProperty("平均响应时间(s)")
status.setTestResponseTime(ApiStatusVO.getApiRt());
// @ExcelProperty("最大响应时间(s)")
status.setTestMaxResponseTime("102");
// @ExcelProperty("错误率(%)")
status.setTestErrorRate("13.18");
// @ExcelProperty("数据库CPU(User)(%)")
status.setTestCpuPercentum("15.18");
// @ExcelProperty("数据库TCP连接数")
status.setTestTcpLinkNum("105");
// @ExcelProperty("连接池大小")
status.setTestLinkPooSize("106");
// @ExcelProperty("网络流量(上传/Mbps)(Min)")
status.setTestMinFlow("107");
// @ExcelProperty("网络流量(上传/Mbps)(Max)")
status.setTestMaxFlow("108");
dataList.add(status);
});
return dataList;
}
/**
* 自定义头
* @param headTitle 统一头
* @param headDescTitle 描述头
* @return 返回整个头list。 头部相同连续的单元格会自动合并。
*/
private static List<List<String>> head(String headTitle, String headDescTitle){
List<List<String>> list = new ArrayList<List<String>>();
// 压测名称 压测时间 压测时常(s)平均吞吐量(s) 平均响应时间(s) 最大响应时间(s) 错误率(%) 数据库CPU(User)(%) 数据库TCP连接数 连接池大小 网络流量(上传/Mbps) 网络流量(下载/Mbps)
List<String> head1 = new ArrayList<String>();
head1.add(headTitle);
head1.add(headDescTitle);
head1.add(headDescTitle);
head1.add(headDescTitle);
head1.add(headDescTitle);
head1.add(headDescTitle);
head1.add(headDescTitle);
head1.add("压测名称");
List<String> head2 = new ArrayList<String>();
head2.add(headTitle);
head2.add(headDescTitle);
head2.add(headDescTitle);
head2.add(headDescTitle);
head2.add(headDescTitle);
head2.add(headDescTitle);
head2.add(headDescTitle);
head2.add("压测时间");
List<String> head3 = new ArrayList<String>();
head3.add(headTitle);
head3.add(headDescTitle);
head3.add(headDescTitle);
head3.add(headDescTitle);
head3.add(headDescTitle);
head3.add(headDescTitle);
head3.add(headDescTitle);
head3.add("压测时常(s)");
List<String> head4 = new ArrayList<String>();
head4.add(headTitle);
head4.add(headDescTitle);
head4.add(headDescTitle);
head4.add(headDescTitle);
head4.add(headDescTitle);
head4.add(headDescTitle);
head4.add(headDescTitle);
head4.add("平均吞吐量(s)");
List<String> head5 = new ArrayList<String>();
head5.add(headTitle);
head5.add(headDescTitle);
head5.add(headDescTitle);
head5.add(headDescTitle);
head5.add(headDescTitle);
head5.add(headDescTitle);
head5.add(headDescTitle);
head5.add("平均响应时间(s)");
List<String> head6 = new ArrayList<String>();
head6.add(headTitle);
head6.add(headDescTitle);
head6.add(headDescTitle);
head6.add(headDescTitle);
head6.add(headDescTitle);
head6.add(headDescTitle);
head6.add(headDescTitle);
head6.add("最大响应时间(s)");
List<String> head7 = new ArrayList<String>();
head7.add(headTitle);
head7.add(headDescTitle);
head7.add(headDescTitle);
head7.add(headDescTitle);
head7.add(headDescTitle);
head7.add(headDescTitle);
head7.add(headDescTitle);
head7.add("错误率(%)");
List<String> head8 = new ArrayList<String>();
head8.add(headTitle);
head8.add(headDescTitle);
head8.add(headDescTitle);
head8.add(headDescTitle);
head8.add(headDescTitle);
head8.add(headDescTitle);
head8.add(headDescTitle);
head8.add("数据库CPU(User)(%)");
List<String> head9 = new ArrayList<String>();
head9.add(headTitle);
head9.add(headDescTitle);
head9.add(headDescTitle);
head9.add(headDescTitle);
head9.add(headDescTitle);
head9.add(headDescTitle);
head9.add(headDescTitle);
head9.add("数据库TCP连接数");
List<String> head10 = new ArrayList<String>();
head10.add(headTitle);
head10.add("负责人:");
head10.add("齐冰洋");
head10.add("");
head10.add("");
head10.add("");
head10.add("");
head10.add("连接池大小");
List<String> head11 = new ArrayList<String>();
head11.add(headTitle);
head11.add("协助人:");
head11.add("石星");
head11.add("张刚强");
head11.add("秦亚飞");
head11.add("王京朝");
head11.add("张凯");
head11.add("网络流量(上传/Mbps)");
List<String> head12 = new ArrayList<String>();
head12.add(headTitle);
head12.add("");
head12.add("黄色:瓶颈");
head12.add("橙色:提醒");
head12.add("红色:严重");
head12.add("测试时间");
head12.add("2020-01-01"); //测试时间
head12.add("网络流量(下载/Mbps)");
list.add(head1);
list.add(head2);
list.add(head3);
list.add(head4);
list.add(head5);
list.add(head6);
list.add(head7);
list.add(head8);
list.add(head9);
list.add(head10);
list.add(head11);
list.add(head12);
return list;
}
}
拦截,写入样式、控制列宽:
package com.kit;
import ……public class CustomCellWriteHandler extends AbstractCellStyleStrategy implements CellWriteHandler {
private static final Integer width = 18;
Workbook workbooks;
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean isHead) {
this.initCellStyle(writeSheetHolder.getSheet().getWorkbook());
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), width * 256);
this.setHeadCellStyle(cell,head,integer);
}
@Override
protected void initCellStyle(Workbook workbook) {
this.workbooks = workbook;
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer integer) {
//头部Top样式
if(cell.getRowIndex()==0){
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,16));
}else if((cell.getRowIndex()==1 && cell.getColumnIndex()>8)){
//负责人 、协助人
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.GREEN));
}else if(cell.getColumnIndex() == 11 && (cell.getRowIndex() == 2 || cell.getRowIndex() == 3 || cell.getRowIndex() == 4 || cell.getRowIndex() == 5)){
//警告
switch (cell.getRowIndex()){
case 2:
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.YELLOW));
break;
case 3:
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.ORANGE));
break;
case 4:
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.RED));
break;
case 5:
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.GREEN));
break;
}
}else if(cell.getRowIndex() == 7 && cell.getColumnIndex() == 0 ){
//标题底色
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.GREEN));
}else if(cell.getRowIndex() == 7 && cell.getColumnIndex() != 0 ){
//标题底色
cell.setCellStyle(PoiUtils.getColumnTopStyle(workbooks,IndexedColors.ORANGE));
}else {
cell.setCellStyle(PoiUtils.getColumnStyle(workbooks));
}
if(cell.getRowIndex() > 7){
cell.setCellStyle(PoiUtils.getColumnStyle(workbooks));
}
}
@Override
protected void setContentCellStyle(Cell cell, Head head, Integer integer) {
}
}
样式设置: 此处代码参考来源:https://blog.csdn.net/qq_15081901/article/details/90202723 (POI封装工具easyexcel导出EXCEL表样式设置)
package com.kit;
import……public class PoiUtils {
/**
* 首头单元格
* @param workbook
* @return
*/
public static CellStyle getColumnTopStyle(Workbook workbook,int FontSize) {
if(FontSize == 0){
FontSize=12;
}
// 设置字体
Font font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short) FontSize);
//字体加粗
font.setBold(true);
//设置字体名字
font.setFontName("宋体");
//设置样式;
CellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(BorderStyle.THIN);
//设置底边框颜色;
style.setBottomBorderColor((short) 0);
//设置左边框;
style.setBorderLeft(BorderStyle.THIN);
//设置左边框颜色;
style.setLeftBorderColor((short) 0);
//设置右边框;
style.setBorderRight(BorderStyle.THIN);
//设置右边框颜色;
style.setRightBorderColor((short) 0);
//设置顶边框;
style.setBorderTop(BorderStyle.THIN);
//设置顶边框颜色;
style.setTopBorderColor((short) 0);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HorizontalAlignment.CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
}
/**
* 头部自己定义单元格
* @param workbook
* @return
*/
public static CellStyle getColumnTopStyle(Workbook workbook, IndexedColors indexedColors) {
// 设置字体
Font font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 12);
//字体加粗
font.setBold(true);
//设置字体名字
font.setFontName("宋体");
//设置样式;
CellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(BorderStyle.THIN);
//设置底边框颜色;
style.setBottomBorderColor((short) 0);
//设置左边框;
style.setBorderLeft(BorderStyle.THIN);
//设置左边框颜色;
style.setLeftBorderColor((short) 0);
//设置右边框;
style.setBorderRight(BorderStyle.THIN);
//设置右边框颜色;
style.setRightBorderColor((short) 0);
//设置顶边框;
style.setBorderTop(BorderStyle.THIN);
//设置顶边框颜色;
style.setTopBorderColor((short) 0);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HorizontalAlignment.CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(VerticalAlignment.CENTER);
/**
* 背景色
*/
if(indexedColors != null){
style.setFillForegroundColor(indexedColors.getIndex());// 设置背景色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
return style;
}
/*
* 字段样式
*/
public static CellStyle getColumnStyle(Workbook workbook) {
// 设置字体
Font font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体加粗
// font.setBold(true);
//设置字体名字
font.setFontName("宋体");
//设置样式;
CellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(BorderStyle.THIN);
//设置底边框颜色;
style.setBottomBorderColor((short) 0);
//设置左边框;
style.setBorderLeft(BorderStyle.THIN);
//设置左边框颜色;
style.setLeftBorderColor((short) 0);
//设置右边框;
style.setBorderRight(BorderStyle.THIN);
//设置右边框颜色;
style.setRightBorderColor((short) 0);
//设置顶边框;
style.setBorderTop(BorderStyle.THIN);
//设置顶边框颜色;
style.setTopBorderColor((short) 0);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(true);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HorizontalAlignment.CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
}
}
TestReportVO
import …… @Data
public class TestReportVO { @ExcelProperty("压测名称")
private String testName;
@ExcelProperty("压测时间")
private Date testDate;
@ExcelProperty("压测时常(s)")
private String testDuration;
@ExcelProperty("平均吞吐量(s)")
private String testHandlingCapacity;
@ExcelProperty("平均响应时间(s)")
private String testResponseTime ;
@ExcelProperty("最大响应时间(s)")
private String testMaxResponseTime;
@ExcelProperty(错误率(%))
private String testErrorRate ;
@ExcelProperty("数据库CPU(User)(%))
private String testCpuPercentum ;
@ExcelProperty("数据库TCP连接数")
private String testTcpLinkNum;
@ExcelProperty("连接池大小")
private String testLinkPooSize;
@ExcelProperty("网络流量(上传/Mbps)")
private String testMinFlow ;
@ExcelProperty("网络流量(上传/Mbps)")
private String testMaxFlow; }
导出效果:

有问题可以联系 邮箱 253049953@qq.com
TestReportVO
EasyExcel 自定义单元格式的问题。的更多相关文章
- POI对Excel自定义日期格式的读取
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel private List<String[]> rosolveFile(InputStream is, String ...
- java中使用poi导入导出excel文件_并自定义日期格式
Apache POI项目的使命是创造和保持java API操纵各种文件格式基于Office Open XML标准(OOXML)和微软的OLE复合文档格式(OLE2)2.总之,你可以读写Excel文件使 ...
- 使用POI读取xlsx文件,包含对excel中自定义时间格式的处理
package poi; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...
- poi中如何自定义日期格式
1. poi的“Quick Guide”中提供了 “How to create date cells ”例子来说明如何创建日期单元格,代码如下: HSSFCellStyle cellStyle = w ...
- Excel 2007中自定义数字格式前要了解的准则
要在Excel 2007中创建自定义数字格式,首先应了解自定义数字格式的准则,并从选择某一内置数字格式开始.然后,可以更改该格式的任意代码部分,从而创建自己的自定义数字格式. 数字格式最多可包含四个代 ...
- poi处理excel自定义日期格式
poi读取excel自定义时间类型时,读取到的是CELL_TYPE_NUMERIC,即数值类型,这个时候如果直接取值的话会发现取到的值和表格中的值不一样,这时应该先判断值是否是时间或者日期类型再进行处 ...
- Swift - 自定义单元格实现微信聊天界面
1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...
- 浅谈DevExpress<五>:TreeList简单的美化——自定义单元格,加注释以及行序号
今天就以昨天的列表为例,实现以下效果:预算大于110万的单元格突出显示,加上行序号以及注释,如下图:
- OpenResty 自定义 access_log 格式
定义access log的format是 Nginx已经提供的功能,有了 ngx_lua 之后就可以更灵活的记录请求相关的信息,而不仅仅拘泥于 Nginx的内置变量了,可以自定义一些格式和变量来存储结 ...
随机推荐
- Vmware虚拟机用户密码忘记了怎么办?
Vmware虚拟机用户密码忘记了怎么办? 虚拟机Linux上用户密码忘记了怎么办? 今天准备再学习一下linux操作系统,由于之前已经安装过vaware和linux(redhat),所以这次认为不 ...
- P1020 从大到小排序
题目描述 给你n个整数,请你按照从大到小的顺序输出它们. 输入格式 输入的第一行包含一个整数 \(n(1 \le n \le 10^3)\) ,用于表示元素的个数. 输入的第二行包含 \(n\) 个整 ...
- 性能测试基础-SOCKET协议用例
1.首先在进行性能测试的时候,我们要了解软件的通信协议是什么,我们使用什么协议,如何去模拟.SOCKET协议主要应用于在C/S模式的系统. 作者本人已当初做过的C/S架构的系统做的脚本录制,在上面做脚 ...
- JWT之登录、登出、验证码接口
6.2 验证码接口 验证码接口用于登录页面展示时,获取验证码图片地址及验证码标识 安装验证码功能组件(如果是官网下载的完整版框架,无需安装) composer require topthink/thi ...
- dotnet 特性 DynamicallyInvokable 是用来做什么的
我在 Linq 很多函数都看到 __DynamicallyInvokable 这个特性,这是一个没有官方文档的特性,也许是用来优化反射 在堆栈 网找到了以下描述 这个 __DynamicallyInv ...
- json文件生成
// import Translate from 'translate-components' /* * 匹配所有汉字RegExp: [\u4e00-\u9fa5] [\u4E00-\u9FA5]|[ ...
- Android CTS中neverallow规则生成过程
CTS里面SELinux相关测试中neverallow测试项占绝大多数,Android系统开发者都应该知道,在修改sepolicy时,需要确保不能违反这些neverallow规则,不然会过不了CTS. ...
- ulimit -u 解决 Jenkins OOM 错误
Apr 24, 2018 11:19:48 AM hudson.init.impl.InstallUncaughtExceptionHandler$DefaultUncaughtExceptionHa ...
- $bzoj2560$ 串珠子 容斥+$dp$
正解:容斥+$dp$ 解题报告: 传送门$QwQ$ $umm$虽然题目蛮简练的了但还是有点难理解,,,我再抽象一点儿,就说有$n$个点,点$i$和点$j$之间有$a_{i,j}$条无向边可以连,问有多 ...
- $Poj2956/AcWing116\ The\ Pilots\ Brothers'Refrigerator$ 二进制
AcWing $Sol$ 假设改变$[x1,y1]$和$[x2,y2]$的状态就可以达到目的.注意到先改变谁对结果是没有影响的!! 所以就可以直接枚举改变状态的结点而不需要注意顺序. $4*4$的矩阵 ...