Java JXLS导出Excel(maven项目)

参考文档地址: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     }

 

posted @ 2020-04-28 11:05  xc888  阅读(1844)  评论(0)    收藏  举报