EasyPoi 简单导入导出

时间:2024-01-30 09:06:32

导入逻辑

1 public ResultMsg importUser(MultipartFile file) throws Exception {
2         ImportParams params = new ImportParams();
3         params.setHeadRows(1);
4         List<UserImportExcel> list = ExcelImportUtil.importExcel(file.getInputStream(), UserImportExcel.class, params);
5         list.forEach(user -> log.info(JSONObject.toJSONString(user)));
6         return null;
7     }

导出逻辑

 1 public void exportUser(@RequestBody ListUser param, HttpServletResponse response) throws IOException {
 2         List<UserExportExcel> result = service.listNoLimit(param);
 3         response.setContentType("application/vnd.ms-excel");
 4         response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
 5         @Cleanup
 6         OutputStream ouputStream = null;
 7         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户导出", "用户"), UserExportExcel.class, result);
 8         try {
 9             ouputStream = response.getOutputStream();
10             workbook.write(ouputStream);
11         } catch (IOException e) {
12             throw e;
13         } finally {
14             IOUtils.closeQuietly(ouputStream);
15             IOUtils.closeQuietly(workbook);
16         }
17     }

操作实体类

 1 package com.user.model;
 2 
 3 import cn.afterturn.easypoi.excel.annotation.Excel;
 4 import lombok.Data;
 5 
 6 /**
 7  * @author SZRD_A2 SXT
 8  * @version V1.0
 9  * @date 2020/12/29 0029 15:48
10  */
11 
12 @Data
13 public class UserImportExcel {
14 
15     @Excel(name = "姓名", width = 24, fixedIndex = 1, orderNum = "1")
16     private String name;
17 
18     @Excel(name = "工号", width = 24, fixedIndex = 2, orderNum = "2")
19     private String id;
20 
21     @Excel(name = "手机号", width = 24, fixedIndex = 3, orderNum = "3")
22     private String phone;
23 
24     @Excel(name = "部门", width = 24, fixedIndex = 4, orderNum = "4")
25     private String department;
26 }

maven配置

 1 <properties>
 2 <easypoi.version>4.2.0</easypoi.version>
 3 <apache.poi.version>4.1.2</apache.poi.version>
 4 </properties>
 5 
 6 
 7 <dependencies>
 8 
 9 <!-- easypoi -->
10         <dependency>
11             <groupId>cn.afterturn</groupId>
12             <artifactId>easypoi-base</artifactId>
13             <version>${easypoi.version}</version>
14             <exclusions>
15                 <exclusion>
16                     <groupId>com.google.guava</groupId>
17                     <artifactId>guava</artifactId>
18                 </exclusion>
19             </exclusions>
20         </dependency>
21         <dependency>
22             <groupId>cn.afterturn</groupId>
23             <artifactId>easypoi-web</artifactId>
24             <version>${easypoi.version}</version>
25         </dependency>
26         <dependency>
27             <groupId>cn.afterturn</groupId>
28             <artifactId>easypoi-annotation</artifactId>
29             <version>${easypoi.version}</version>
30         </dependency>
31 
32         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
33         <dependency>
34             <groupId>org.apache.poi</groupId>
35             <artifactId>poi</artifactId>
36             <version>${apache.poi.version}</version>
37         </dependency>
38 
39         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
40         <dependency>
41             <groupId>org.apache.poi</groupId>
42             <artifactId>poi-ooxml</artifactId>
43             <version>${apache.poi.version}</version>
44         </dependency>
45         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
46         <dependency>
47             <groupId>org.apache.poi</groupId>
48             <artifactId>poi-ooxml-schemas</artifactId>
49             <version>${apache.poi.version}</version>
50         </dependency>
51 </dependencies>