*************************************優雅的分割線 **********************************
分享一波:程序員賺外快-必看的巔峰干貨
如果以上內容對你覺得有用,并想獲取更多的賺錢方式和免費的技術教程
請關注微信公眾號: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 +'}';}
}
編寫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 {@Autowiredprivate ApiMapper apiMapper;/*** 導出測試* @throws Exception*/@Testpublic 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*/@Testpublic 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;}
*************************************優雅的分割線 **********************************
分享一波:程序員賺外快-必看的巔峰干貨
如果以上內容對你覺得有用,并想獲取更多的賺錢方式和免費的技術教程
請關注微信公眾號:HB荷包
一個能讓你學習技術和賺錢方法的公眾號,持續更新
*************************************優雅的分割線 **********************************
SimpleExecutor