关于EasyPoi导出Excel Springmvc导出Excel(maven)
如果你觉得Easypoi不好用,喜欢用传统的poi,可以参考我的这篇博客:Springmvc导出Excel(maven) 当然了,万变不离其宗。Easypoi的底层原理还是poi。正如MyBatis Plus的原理还是MyBatis那套。只不过它们的共同点是封装起来。 关于Easypoi记得初次接触的时候,给我的感觉是看起来很简单很容易让人理解,而且文档也比较丰富,也是jeecg的开源项目下的子项目。 记得当初为了提高代码开发效率,去码云和github上游荡游荡,突然发现了一个叫jeecg的玩意,于是研究了下,这个研究不是特别深,只是将其项目跑起来,看看它有哪些组件,顺便看看源码,和玩玩它强大的插件式开发和easypoi。 jeecg中的easypoi的项目地址为:https://gitee.com/jeecg/jeasypoi jeecg中的easypoi的官方详细文档为:http://easypoi.mydoc.io/ ? 大家要牢记一个经济方面的格言:物质基础决定上层建筑。 对计算机专业的同志们而言,良好的计算机基础,是以后编程世界的驰骋飞扬的基石。 所以在校的同志们,一定要好好学习天天向上。 闲话就不多说了,下面进入正题。 ? 一、导入依赖 <!-- 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> ? 二、构建实体 @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" String orderNo; /** * 用户id */ @TableField("user_id" String userId; * 交易创建时间 @Excel(name="创建时间",width=30) @TableField("create_time" String createTime; * 实际支付金额 @Excel(name="实际支付金额" BigDecimal amount; * 交易状态支付成功转入退款未支付已关闭已撤销支付失败 */ String status; * 流水标题 String subject; @TableField("finish_time" String finishTime; * 类型:余额支付balance、微信支付wx String type; * 资金流向:1:收入 ;0:支出 String flows; * 备注 String remarks; * 支付类型:余额,微信,混合支付等 @TableField(exist=false) @Excel(name="支付类型" String payType; * 费用名称 ) @Excel(name="费用名称" String amountType; @TableField(exist= String leafNode; @TableField(exist= String address; @TableField(exist= String node; @TableField(exist= String subNode; @TableField(exist=) @Excel(name="账户",1)">public String loginCode; @TableField(exist=) @Excel(name="支付人" String userName; * 公司编码 * @return @TableField(value="company_code" String companyCode; * 用户 ) List<SysUser> sysUser; * 订单 ) List<FinanceOrder> order; String getLoginCode() { return loginCode; } void setLoginCode(String loginCode) { this.loginCode = String getUserName() { userName; } setUserName(String userName) { this.userName = String getLeafNode() { leafNode; } setLeafNode(String leafNode) { this.leafNode = String getAddress() { address; } setAddress(String address) { this.address = String getNode() { node; } setNode(String node) { this.node = String getSubNode() { subNode; } setSubNode(String subNode) { this.subNode = String getPayType() { payType; } setPayType(String payType) { this.payType = String getAmountType() { amountType; } setAmountType(String amountType) { this.amountType = amountType; } String getCompanyCode() { companyCode; } setCompanyCode(String companyCode) { this.companyCode = String getTradeNo() { tradeNo; } setTradeNo(String tradeNo) { this.tradeNo = String getOrderNo() { orderNo; } setOrderNo(String orderNo) { this.orderNo = String getUserId() { userId; } setUserId(String userId) { this.userId = String getCreateTime() { createTime; } setCreateTime(String createTime) { this.createTime = BigDecimal getAmount() { amount; } setAmount(BigDecimal amount) { this.amount = String getStatus() { status; } setStatus(String status) { this.status = String getSubject() { subject; } setSubject(String subject) { this.subject = String getFinishTime() { finishTime; } setFinishTime(String finishTime) { this.finishTime = String getType() { type; } setType(String type) { this.type = String getFlows() { flows; } setFlows(String flows) { this.flows = String getRemarks() { remarks; } setRemarks(String remarks) { this.remarks = remarks; } @Override protected Serializable pkVal() { return this.tradeNo; } public List<SysUser> getSysUser() { sysUser; } void setSysUser(List<SysUser> sysUser) { this.sysUser =public List<FinanceOrder> getOrder() { order; } void setOrder(List<FinanceOrder> order) { this.order = order; } @Override 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 + "]"; } }
? ? 上面截图我只是截取官方文档上的一部分,大家想详细知道,可参考官方文档:http://easypoi.mydoc.io/#text_197835 ? 三、编写对应的Controller @Controller @RequestMapping(value = "easypoi"class ExportExcelTest { @Autowired FinanceTradeService financeTradeService; * 导出Excel 营业收入支出明细 * @param companyCode * flows * response * @return @GetMapping(value="exportBillDatailInfo") @ResponseBody 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",1)">); 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导出 * Workbook exportSheets(String companyCode,String flows){ 将条件放入Map中 Map<String,Object> conditionMap = new HashMap<String,Object>(); conditionMap.put("companyCode",companyCode); conditionMap.put("flows"); conditionMap.put("size",100); 获得营业明细(含收支)信息 List<FinanceTrade> list = financeTradeService.getSpeedingDetailInfo(conditionMap); 创建参数对象(用来设定excel得sheet得内容等信息) ExportParams params1 = ExportParams() ; 设置sheet得名称 params1.setSheetName("营业收支明细"); ; 创建sheet1使用得map Map<String,Object> dataMap1 = new HashMap<>(); title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName dataMap1.put("title" 模版导出对应得实体类型 dataMap1.put("entity",FinanceTrade.) ; sheet中要填充得数据 dataMap1.put("data" 将sheet1和sheet2使用得map进行包装 List<Map<String,Object>> sheetsList = new ArrayList<>() ; sheetsList.add(dataMap1); 执行方法 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(该例非常简单,很通俗易懂) ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |