您好,登錄后才能下訂單哦!
本篇內容介紹了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。