微信链接网页网站制作,个人做网站开发指标,优化 导航网站,无极网站维护*************************************优雅的分割线 **********************************
分享一波:程序员赚外快-必看的巅峰干货
如果以上内容对你觉得有用,并想获取更多的赚钱方式和免费的技术教程
请关注微信公众号:HB荷包 一个能让你学习技术和赚钱方法的公众号,持续更…*************************************优雅的分割线 **********************************
分享一波:程序员赚外快-必看的巅峰干货
如果以上内容对你觉得有用,并想获取更多的赚钱方式和免费的技术教程
请关注微信公众号:HB荷包 一个能让你学习技术和赚钱方法的公众号,持续更新 *************************************优雅的分割线 ********************************** SimpleExecutor 最近在搭公司新项目的架构测试的过程中深感导出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;Overridepublic 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 };}
}编写MapperService就跳过了
Component
public interface ApiMapper {/*** 查询所有* return*/ListApiLog findAll();}?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.gej.poi.mapper.ApiMapper!-- 注意本内容仅限于风越云力内部传阅禁止外泄以及用于其他的商业目 --!-- 通用查询映射结果 --resultMap idBaseResultMap typecom.gej.poi.pojo.ApiLogid columnlog_id propertylogId/result columnlog_url propertylogUrl/result columnlog_params propertylogParams/result columnlog_status propertylogStatus/result columnlog_message propertylogMessage/result columnlog_ua propertylogUa/result columnlog_controller propertylogController/result columnlog_method propertylogMethod/result columnlog_time propertylogTime/result columnlog_ip propertylogIp/result columnlog_device propertylogDevice/result columncreated_date propertycreatedDate/result columncreated_by propertycreatedBy/result columnlog_result propertylogResult/result columncreated_name propertycreatedName//resultMap!-- 通用查询结果列 --sql idBase_Column_Listlog_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/sqlselect idfindAll resultMapBaseResultMapselect * from sys_log_api/select/mapper编写测试类 SpringBootTest
RunWith(SpringRunner.class)
public class ExportTest {Autowiredprivate ApiMapper apiMapper;/*** 导出测试* throws Exception*/Testpublic void testExportLog() throws Exception {ListApiLog 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*/Testpublic void testImportLog() throws Exception {InputStream inputStream new FileInputStream(new File(D:/测试.xlsx));ListApiLog 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;}*************************************优雅的分割线 **********************************
分享一波:程序员赚外快-必看的巅峰干货
如果以上内容对你觉得有用,并想获取更多的赚钱方式和免费的技术教程
请关注微信公众号:HB荷包 一个能让你学习技术和赚钱方法的公众号,持续更新 *************************************优雅的分割线 ********************************** SimpleExecutor