Easypoi 輕松實現(xiàn)復雜excel文件導出功能
之前做的excel文件導出都相對簡單,用的都是公司自己封裝的一些poi方法,導出內(nèi)容都是表頭+一行行的數(shù)據(jù),但是這次需要導出的excel復雜度高了不少,自己用現(xiàn)有方法做比較麻煩,因此引入了Easypoi 進行實現(xiàn)。
之所以用Easypoi我是看中了它可以直接根據(jù)現(xiàn)有模板填充數(shù)據(jù)實現(xiàn)excel生成,而模板產(chǎn)品已經(jīng)給出,那么接下來只需要稍微做修改就能使用,修改后的模板如圖:

對照著官方文檔的示例(http://easypoi.mydoc.io/ ) 很容易就能理解模板的使用規(guī)則
注意其中的{{$fe: 是遍歷listmap變量循環(huán)填充數(shù)據(jù)行,其他則是類似占位符,很好理解
//代碼就純粹只是把模板的占位符和變量對應起來
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構造函數(shù)只支持傳入文件路徑,而直接傳入resource下的模板文件路徑在線上環(huán)境會有問題(本地沒問題),因此需要用getResourceAsStream讀取后生成臨時文件,再把臨時文件url傳入
到此這篇關于Easypoi 輕松實現(xiàn)復雜excel文件導出功能的文章就介紹到這了,更多相關Easypoi導出復雜excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解CopyOnWriteArrayList是如何保證線程安全
這篇文章主要為大家介紹了CopyOnWriteArrayList是如何保證線程安全講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
解決springMVC 跳轉js css圖片等靜態(tài)資源無法加載的問題
下面小編就為大家?guī)硪黄鉀QspringMVC 跳轉js css圖片等靜態(tài)資源無法加載的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
springboot配置開發(fā)和測試環(huán)境并添加啟動路徑方式
這篇文章主要介紹了springboot配置開發(fā)和測試環(huán)境并添加啟動路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
基于Springboot+Vue實現(xiàn)的在線答題闖關系統(tǒng)全過程
這篇文章主要介紹了基于Springboot+Vue實現(xiàn)的在線答題闖關系統(tǒng)的相關資料,文中包括前端Vue.js、后端SpringBoot及MySQL數(shù)據(jù)庫的使用,系統(tǒng)功能涵蓋順序出題、體型練習、隨機出題、錯題本、收藏題和答題統(tǒng)計等,需要的朋友可以參考下2024-12-12

