參考文檔地址:https://www.cnblogs.com/Crysta1/p/10250270.html
https://blog.csdn.net/hellojackyang/article/details/81065756
https://www.jianshu.com/p/7a97848e8961
1.將需要的包導入pom.xml文件
1 <dependency> 2 <groupId>org.jxls</groupId> 3 <artifactId>jxls</artifactId> 4 <version>2.4.6</version> 5 </dependency> 6 <dependency> 7 <groupId>org.jxls</groupId> 8 <artifactId>jxls-poi</artifactId> 9 <version>1.0.15</version> 10 </dependency> 11 <dependency> 12 <groupId>org.jxls</groupId> 13 <artifactId>jxls-jexcel</artifactId> 14 <version>1.0.7</version> 15 </dependency> 16 <dependency> 17 <groupId>net.sf.jxls</groupId> 18 <artifactId>jxls-core</artifactId> 19 <version>1.0.5</version> 20 </dependency>
2.在桌面新建一個Excel表格編寫導出Excel的模板,如下圖:
圖一
圖二
圖一中 jx:area(lastCell=”L2”)表示表格渲染的范圍,粉色框是Excel的批注,右擊選中的單元格,點擊插入批注即可生成。
圖二中 jx:each(items=”LabelUser” var=”LabelUser” lastCell=”L2”)表示要渲染的數據,其中items的值一定要與方法中返回的集合的“鍵”保持一致
代碼:
1 public JsonResult<String> export(HttpServletResponse response,LabelUserCountQuery condtion) { 2 /** 3 * 1)需要用你自己編寫一個的excel模板 4 * 2)通常excel導出需要關聯更多數據,因此labelDataService.queryByCondition方法經常不符合需求,需要重寫一個為模板導出的查詢 5 * 3)參考ConsoleDictController來實現模板導入導出 6 */ 7 String excelTemplate ="excelTemplates/cms/labelUser/user.xls"; 8 PageQuery<LabelUser> page = condtion.getPageQuery(); 9 //取出全部符合條件的 10 page.setPageSize(Integer.MAX_VALUE); 11 page.setPageNumber(1); 12 page.setTotalRow(Integer.MAX_VALUE); 13 //本次導出需要的數據 14 List<LabelUser> list =labelUserService.countLabelUser(page).getList(); 15 if(list.size()>0) { 16 for(int i=0; i<list.size(); i++) { 17 if(list.get(i).getChangenum()==null||list.get(i).getChangenum().equals("")) { 18 list.get(i).setChangenum("0"); 19 } 20 if(list.get(i).getChangesumnum()==null||list.get(i).getChangesumnum().equals("")) { 21 list.get(i).setChangesumnum("0"); 22 } 23 if(list.get(i).getVerifynum()==null||list.get(i).getVerifynum().equals("")) { 24 list.get(i).setVerifynum("0"); 25 } 26 if(list.get(i).getVerifysumnum()==null||list.get(i).getVerifysumnum().equals("")) { 27 list.get(i).setVerifysumnum("0"); 28 } 29 if(condtion.getUpdateTime()==null||condtion.getUpdateTime().equals("")){ 30 list.get(i).setUpdateTimeStart(""); 31 list.get(i).setUpdateTimeEnd(""); 32 }else{ 33 String str=condtion.getUpdateTime().substring(0, condtion.getUpdateTime().indexOf("至")); 34 list.get(i).setUpdateTimeStart(str); 35 list.get(i).setUpdateTimeEnd(condtion.getUpdateTime().substring(str.length()+1, condtion.getUpdateTime().length())); 36 } 37 38 } 39 } 40 41 try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) { 42 if(is==null) { 43 throw new PlatformException("模板資源不存在:"+excelTemplate); 44 } 45 FileItem item = fileService.createFileTemp("數據_"+DateUtil.now("yyyyMMddHHmmss")+".xls"); 46 OutputStream os = item.openOutpuStream(); 47 Context context = new Context(); 48 //其中LabelUser要與items的值一致(即items的值為LabelUser) 49 context.putVar("LabelUser", list); 50 JxlsHelper.getInstance().processTemplate(is, os, context); 51 os.close(); 52 //下載參考FileSystemContorller 53 return JsonResult.success(item.getPath()); 54 } catch (IOException e) { 55 throw new PlatformException(e.getMessage()); 56 } 57 58 }