本篇内容介绍了“Easypoi如何实现复杂excel文件导出功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
之所以用Easypoi我是看中了它可以直接根据现有模板填充数据实现excel生成,而模板产品已经给出,那么接下来只需要稍微做修改就能使用,修改后的模板如图:
注意其中的{{$fe: 是遍历listmap变量循环填充数据行,其他则是类似占位符,很好理解
//代码就纯粹只是把模板的占位符和变量对应起来 Map<String, Object> map = new HashMap<String, Object>(); map.put("code", customer.getCode()); map.put("name", customer.getName()); map.put("mobile", customer.getMobile()); map.put("membershipGrade", customer.getMembershipGrade()); map.put("zzyMembershipGrade", customer.getZzyMembershipGrade()); map.put("addressComplete", customer.getAddressComplete()); List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(); map.put("maplist", listMap); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (MainOrderSubOrderFinanceResult financeResult : mainOrderSubOrderFinanceResults) { Map<String, String> lm = new HashMap<String, String>(); lm.put("orderCode", financeResult.getOrderCode()); lm.put("payStatusDesc", financeResult.getPayStatusDesc()); lm.put("settlementTypeDesc", financeResult.getSettlementTypeDesc()); lm.put("billDate", dateFormat.format(financeResult.getBillDate())); lm.put("totalAmount", financeResult.getTotalAmount().stripTrailingZeros().toPlainString()); lm.put("discountAmount", financeResult.getDiscountAmount().stripTrailingZeros().toPlainString()); lm.put("amount", financeResult.getAmount().stripTrailingZeros().toPlainString()); lm.put("payAmount", financeResult.getPayAmount().stripTrailingZeros().toPlainString()); lm.put("amountPaid", financeResult.getAmountPaid().stripTrailingZeros().toPlainString()); listMap.add(lm); sumTotalAmount = sumTotalAmount.add(financeResult.getTotalAmount()); sumDiscountAmount = sumDiscountAmount.add(financeResult.getDiscountAmount()); sumAmount = sumAmount.add(financeResult.getAmount()); sumPayAmount = sumPayAmount.add(financeResult.getPayAmount()); sumAmountPaid = sumAmountPaid.add(financeResult.getAmountPaid()); } map.put("sumTotalAmount", sumTotalAmount); map.put("sumDiscountAmount", sumDiscountAmount); map.put("sumAmount", sumAmount); map.put("sumPayAmount", sumPayAmount); map.put("sumAmountPaid", sumAmountPaid); //生成临时模板文件 String url=""; try { File temp = File.createTempFile("exportCreditBillResult", ".xlsx"); FileUploadHelper.inputStream2File(this.getClass().getClassLoader().getResourceAsStream("exportCreditBillResult.xlsx"),temp); url=temp.getAbsolutePath(); temp.deleteOnExit(); } catch (IOException e) { } TemplateExportParams params = new TemplateExportParams(url); Workbook workbook = ExcelExportUtil.exportExcel(params, map);
做这个功能有一个关键点:
由于TemplateExportParams构造函数只支持传入文件路径,而直接传入resource下的模板文件路径在线上环境会有问题(本地没问题),因此需要用getResourceAsStream读取后生成临时文件,再把临时文件url传入
“Easypoi如何实现复杂excel文件导出功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。