最近在搭公司新项目的架构,测试的过程中深感导出Excel极为不便,因此就产生了写一个通用导出工具类的想法。写完后经测试发现比较好用,因此将公司相关的代码移除,单独拿出来这个模块进行开源。
项目的GitHub地址:POI操作工具
如果您对本工具比较感兴趣,可以加入下面QQ群进行技术交流:781943947
使用方式:
创建数据库(这个操作就不贴代码了)
导入工程下db目录的数据库
创建类ApiLog(实际开发中换成自己的类),加上注解@Excel
@Data
public class ApiLog implements Serializable {
private static final long serialVersionUID = -3286564461647015367L;
/**
* 日志id
*/
@Excel(name = "编号")
private Integer logId;
/**
* 请求路径
*/
@Excel(name = "请求地址")
private String logUrl;
/**
* 参数
*/
@Excel(name = "请求参数")
private String logParams;
/**
* 访问状态,1正常0异常
*/
@Excel(name = "访问状态")
private Integer logStatus;
/**
* 异常信息
*/
@Excel(name = "异常信息")
private String logMessage;
/**
* 浏览器UA标识
*/
@Excel(name = "浏览器标识", autoSize = true)
private String logUa;
/**
* 访问controller
*/
@Excel(name = "控制层")
private String logController;
/**
* 请求方式,get、post等
*/
@Excel(name = "请求方式")
private String logMethod;
/**
* 响应时间,单位毫秒
*/
@Excel(name = "响应时间", isStatistics = true)
private Long logTime;
/**
* 请求ip
*/
@Excel(name = "请求ip")
private String logIp;
/**
* 设备MAC
*/
@Excel(name = "设备号")
private String logDevice;
/**
* 创建时间
*/
@Excel(name = "请求时间")
private String createdDate;
/**
* 创建人
*/
private String createdBy;
/**
* 创建人姓名
*/
@Excel(name = "创建人", autoSize = true)
private String createdName;
/**
* 返回值
*/
@Excel(name = "返回值")
private String logResult;
/**
* 日志内容
*/
@Excel(name = "日志内容")
private String logContent;
/**
* 日志类型 0:操作日志;1:登录日志;2:定时任务;
*/
private Integer logType;
/**
* 操作类型 1查询,2添加,3修改,4删除,5导入,6导出
*/
private Integer logOperateType;
@Override
public String toString() {
return "ApiLog{" +
"logId=" + logId +
", logUrl='" + logUrl + '\'' +
", logParams='" + logParams + '\'' +
", logStatus=" + logStatus +
", logMessage='" + logMessage + '\'' +
", logUa='" + logUa + '\'' +
", logController='" + logController + '\'' +
", logMethod='" + logMethod + '\'' +
", logTime=" + logTime +
", logIp='" + logIp + '\'' +
", logDevice='" + logDevice + '\'' +
", createdDate='" + createdDate + '\'' +
", createdBy='" + createdBy + '\'' +
", createdName='" + createdName + '\'' +
", logResult='" + logResult + '\'' +
", logContent='" + logContent + '\'' +
", logType=" + logType +
", logOperateType=" + logOperateType +
'}';
}
}
编写Mapper(Service就跳过了)
@Component
public interface ApiMapper {
/**
* 查询所有
* @return
*/
List<ApiLog> findAll();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gej.poi.mapper.ApiMapper">
<!-- 注意:本内容仅限于风越云力内部传阅,禁止外泄以及用于其他的商业目 -->
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.gej.poi.pojo.ApiLog">
<id column="log_id" property="logId"/>
<result column="log_url" property="logUrl"/>
<result column="log_params" property="logParams"/>
<result column="log_status" property="logStatus"/>
<result column="log_message" property="logMessage"/>
<result column="log_ua" property="logUa"/>
<result column="log_controller" property="logController"/>
<result column="log_method" property="logMethod"/>
<result column="log_time" property="logTime"/>
<result column="log_ip" property="logIp"/>
<result column="log_device" property="logDevice"/>
<result column="created_date" property="createdDate"/>
<result column="created_by" property="createdBy"/>
<result column="log_result" property="logResult"/>
<result column="created_name" property="createdName"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
log_id, log_url, log_params, log_status, log_message, log_ua, log_controller, log_method, log_time, log_ip, log_device, created_date, created_name, log_result
</sql>
<select id="findAll" resultMap="BaseResultMap">
select * from sys_log_api
</select>
</mapper>
编写测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class ExportTest {
@Autowired
private ApiMapper apiMapper;
/**
* 导出测试
* @throws Exception
*/
@Test
public void testExportLog() throws Exception {
List<ApiLog> list = apiMapper.findAll();
Workbook workbook = new ExcelExportHandler().createSheet(new ExportParams("测试导出", "最新日志"), ApiLog.class, list);
OutputStream outputStream = new FileOutputStream(new File("D:/测试.xlsx"));
workbook.write(outputStream);
}
/**
* 导入测试
* @throws Exception
*/
@Test
public void testImportLog() throws Exception {
InputStream inputStream = new FileInputStream(new File("D:/测试.xlsx"));
List<ApiLog> apiLogs = new ExcelImportHandler().importExcel(inputStream, ApiLog.class, new ImportParams());
for (ApiLog apiLog : apiLogs) {
System.out.println(apiLog);
}
}
}
最后附上Excel注解的代码
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Excel {
/**
* 该列是否需要时间格式化
*/
boolean needFormat() default false;
/**
* 时间格式化
*/
String format() default "";
/**
* 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符
*/
double height() default 10;
/**
* 导出时的列名。不可重复
*/
String name();
/**
* 导出时在excel中每个列的宽 单位为字符,一个汉字=2个字符 如 以列名列内容中较合适的长度 例如姓名列6 【姓名一般三个字】
* 性别列4【男女占1,但是列标题两个汉字】 限制1-255
*/
double width() default 10;
/**
* 是否自动统计数据,如果是统计,true的话在最后追加一行统计,把所有数据求和
*/
boolean isStatistics() default false;
/**
* 是否设置列宽自适应
*/
boolean autoSize() default false;
}