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的内置变量了,可以自定义一些格式和变量来存储结 ...
随机推荐
- 21个项目玩转深度学习:基于TensorFlow的实践详解02—CIFAR10图像识别
cifar10数据集 CIFAR-10 是由 Hinton 的学生 Alex Krizhevsky 和 Ilya Sutskever 整理的一个用于识别普适物体的小型数据集.一共包含 10 个类别的 ...
- 2018-2-13-win10-uwp-unix-timestamp-时间戳-转-DateTime
title author date CreateTime categories win10 uwp unix timestamp 时间戳 转 DateTime lindexi 2018-2-13 17 ...
- tensorflow在文本处理中的使用——Doc2Vec情感分析
代码来源于:tensorflow机器学习实战指南(曾益强 译,2017年9月)——第七章:自然语言处理 代码地址:https://github.com/nfmcclure/tensorflow-coo ...
- 【codeforces 764A】Taymyr is calling you
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- Centos7网络连接不上:Network is unreachable 解决方案
有朋友的centos7装在虚拟机上挂起后在打开不能正常连接网络,我的也出现了这个问题,试着用dhclient重新分配一下地址,无奈系统提示dhclient正在运行,没办法只能试试其它办法,之后研究了一 ...
- Mybatis的mapper.xml文件详解
1.#{}和${}的区别: #{}表示一个占位符号,#{}接收输入参数,类型可以是简单类型,pojo.hashmap. 如果接收简单类型,#{}中可以写成value或其它名称. ...
- javeweb_学生信息添加系统
在text.jsp中画出界面,以及设置提交选项的限制 <%@ page language="java" contentType="text/html; charse ...
- 0006 列表(ul、ol、dl)
1. 列表标签(重点) 学习目标 理解 无序列表的应用场景 自定义列表的应用场景 应用 无序列表语法 自定义列表语法 问? 前面我们知道表格一般用于数据展示的,但是网页中还是有很多跟表格类似的布局,如 ...
- [梁山好汉说IT] 容器概念在北宋社会的应用
[梁山好汉说IT] 容器概念在北宋社会的应用 0x00 摘要 如何对没有软件开发经验的人解释容器? 集装箱真的能够完美解释容器嘛? 除了集装箱还有其他常见实体能够解释容器嘛? 我找到了一个能够 和集装 ...