若是你以爲Easypoi很差用,喜歡用傳統的poi,能夠參考個人這篇博客:Springmvc導出Excel(maven)html
固然了,萬變不離其宗。Easypoi的底層原理仍是poi。正如MyBatis Plus的原理仍是MyBatis那套。只不過它們的共同點是封裝起來。node
關於Easypoi記得初次接觸的時候,給個人感受是看起來很簡單很容易讓人理解,並且文檔也比較豐富,也是jeecg的開源項目下的子項目。git
記得當初爲了提升代碼開發效率,去碼雲和github上游蕩遊蕩,忽然發現了一個叫jeecg的玩意,因而研究了下,這個研究不是特別深,只是將其項目跑起來,看看它有哪些組件,順便看看源碼,和玩玩它強大的插件式開發和easypoi。github
jeecg中的easypoi的項目地址爲:https://gitee.com/jeecg/jeasypoiweb
jeecg中的easypoi的官方詳細文檔爲:http://easypoi.mydoc.io/編程
你們要牢記一個經濟方面的格言:物質基礎決定上層建築。微信
對計算機專業的同志們而言,良好的計算機基礎,是之後編程世界的馳騁飛揚的基石。mvc
因此在校的同志們,必定要好好學習每天向上。app
閒話就很少說了,下面進入正題。maven
1、導入依賴
<!-- easypoi導入導出excel --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.1.0</version> </dependency>
2、構建實體
@ExcelTarget("FinanceTrade") @TableName("rms_finance_trade") public class FinanceTrade extends Model<FinanceTrade> { private static final long serialVersionUID = 1L; @TableId("trade_no") private String tradeNo; @Excel(name="訂單號") @TableField("order_no") private String orderNo; /** * 用戶id */ @TableField("user_id") private String userId; /** * 交易建立時間 */ @Excel(name="建立時間",width=30) @TableField("create_time") private String createTime; /** * 實際支付金額 */ @Excel(name="實際支付金額") private BigDecimal amount; /** * 交易狀態支付成功轉入退款未支付已關閉已撤銷支付失敗 */ private String status; /** * 流水標題 */ private String subject; @TableField("finish_time") private String finishTime; /** * 類型:餘額支付balance、微信支付wx */ private String type; /** * 資金流向:1:收入 ;0:支出 */ private String flows; /** * 備註 */ private String remarks; /** * 支付類型:餘額,微信,混合支付等 */ @TableField(exist=false) @Excel(name="支付類型") private String payType; /** * 費用名稱 */ @TableField(exist=false) @Excel(name="費用名稱") private String amountType; @TableField(exist=false) private String leafNode; @TableField(exist=false) private String address; @TableField(exist=false) private String node; @TableField(exist=false) private String subNode; @TableField(exist=false) @Excel(name="帳戶",width=30) public String loginCode; @TableField(exist=false) @Excel(name="支付人") public String userName; /** * 公司編碼 * @return */ @TableField(value="company_code") private String companyCode; /** * 用戶 */ @TableField(exist=false) List<SysUser> sysUser; /** * 訂單 */ @TableField(exist=false) List<FinanceOrder> order; public String getLoginCode() { return loginCode; } public void setLoginCode(String loginCode) { this.loginCode = loginCode; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getLeafNode() { return leafNode; } public void setLeafNode(String leafNode) { this.leafNode = leafNode; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getNode() { return node; } public void setNode(String node) { this.node = node; } public String getSubNode() { return subNode; } public void setSubNode(String subNode) { this.subNode = subNode; } public String getPayType() { return payType; } public void setPayType(String payType) { this.payType = payType; } public String getAmountType() { return amountType; } public void setAmountType(String amountType) { this.amountType = amountType; } public String getCompanyCode() { return companyCode; } public void setCompanyCode(String companyCode) { this.companyCode = companyCode; } public String getTradeNo() { return tradeNo; } public void setTradeNo(String tradeNo) { this.tradeNo = tradeNo; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getFinishTime() { return finishTime; } public void setFinishTime(String finishTime) { this.finishTime = finishTime; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getFlows() { return flows; } public void setFlows(String flows) { this.flows = flows; } public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } @Override protected Serializable pkVal() { return this.tradeNo; } public List<SysUser> getSysUser() { return sysUser; } public void setSysUser(List<SysUser> sysUser) { this.sysUser = sysUser; } public List<FinanceOrder> getOrder() { return order; } public void setOrder(List<FinanceOrder> order) { this.order = order; } @Override public String toString() { return "FinanceTrade [tradeNo=" + tradeNo + ", orderNo=" + orderNo + ", userId=" + userId + ", createTime=" + createTime + ", amount=" + amount + ", status=" + status + ", subject=" + subject + ", finishTime=" + finishTime + ", type=" + type + ", flows=" + flows + ", remarks=" + remarks + ", payType=" + payType + ", amountType=" + amountType + ", leafNode=" + leafNode + ", address=" + address + ", node=" + node + ", subNode=" + subNode + ", companyCode=" + companyCode + "]"; } }
- @Excel 做用到filed上面,是對Excel一列的一個描述
- @ExcelCollection 表示一個集合,主要針對一對多的導出,好比一個老師對應多個科目,科目就能夠用集合表示
- @ExcelEntity 表示一個繼續深刻導出的實體,但他沒有太多的實際意義,只是告訴系統這個對象裏面一樣有導出的字段
- @ExcelIgnore 和名字同樣表示這個字段被忽略跳過這個導導出
- @ExcelTarget 這個是做用於最外層的對象,描述這個對象的id,以便支持一個對象能夠針對不一樣導出作出不一樣處理
上面截圖我只是截取官方文檔上的一部分,你們想詳細知道,可參考官方文檔:http://easypoi.mydoc.io/#text_197835
3、編寫對應的Controller
@Controller @RequestMapping(value = "easypoi") public class ExportExcelTest { @Autowired private FinanceTradeService financeTradeService; /** * 導出Excel 營業收入支出明細 * @param companyCode * @param flows * @param response * @return */ @GetMapping(value="exportBillDatailInfo") @ResponseBody public String exportBillDatailInfo(HttpServletRequest request,HttpServletResponse response){ String companyCode = request.getParameter("companyCode"); String flows = request.getParameter("flows"); System.out.println("companyCode:"+companyCode); System.out.println("flows:"+flows); // 獲取workbook對象 // 單sheet或多sheet 只須要更改此處便可 Workbook workbook = exportSheets(companyCode,flows) ; // 判斷數據 if(workbook == null) { return "fail"; } // 設置excel的文件名稱 String excelName = "測試excel" ; // 重置響應對象 response.reset(); // 當前日期,用於導出文件名稱 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]"; // 指定下載的文件名--設置響應頭 response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 寫出數據輸出流到頁面 try { OutputStream output = response.getOutputStream(); BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output); workbook.write(bufferedOutPut); bufferedOutPut.flush(); bufferedOutPut.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } return "success"; } /** * 多sheet導出 * @return */ public Workbook exportSheets(String companyCode,String flows){ //將條件放入Map中 Map<String,Object> conditionMap = new HashMap<String,Object>(); conditionMap.put("companyCode", companyCode); conditionMap.put("flows", flows); conditionMap.put("start", 0); conditionMap.put("size", 100); //得到營業明細(含收支)信息 List<FinanceTrade> list = financeTradeService.getSpeedingDetailInfo(conditionMap); // 建立參數對象(用來設定excel得sheet得內容等信息) ExportParams params1 = new ExportParams() ; // 設置sheet得名稱 params1.setSheetName("營業收支明細"); ; // 建立sheet1使用得map Map<String,Object> dataMap1 = new HashMap<>(); // title的參數爲ExportParams類型,目前僅僅在ExportParams中設置了sheetName dataMap1.put("title",params1) ; // 模版導出對應得實體類型 dataMap1.put("entity",FinanceTrade.class) ; // sheet中要填充得數據 dataMap1.put("data",list) ; // 將sheet1和sheet2使用得map進行包裝 List<Map<String, Object>> sheetsList = new ArrayList<>() ; sheetsList.add(dataMap1); // 執行方法 return ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF) ; } }
小結:上面主要是關於Spring+MyBatis Plus+SpringMVC+EasyPoi+MySQL,更多簡單例子能夠去官方文檔上看或者參照下面博客地址也能夠:
EasyPoi導入導出:https://www.cnblogs.com/xiexy/p/8044393.html
EasyPoi導出示例:http://www.leftso.com/blog/329.html(該例很是簡單,很通俗易懂)