Easypoi导入导出表格



1.引入jar包

1
2
3
4
1.easypoi 父包--作用大家都懂得
2.easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理
3.easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能
4.easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能

非maven工程:直接引入easypoi-base, easypoi-annotation

使用maven,请使用如下坐标:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>

2.简单Demo

bean层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package cn.blue.easypoi.bean;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
* @author Blue
* @date 2020/4/28
**/
@Data
public class UserInfo implements Serializable {
/**
* id
*/
@Excel(name = "id")
private String id;
/**
* 学生姓名
*/
@Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
private String name;
/**
* 学生性别
*/
@Excel(name = "学生性别", replace = {"男_0", "女_1"}, suffix = "生", isImportField = "true_st")
private Integer sex;

@Excel(name = "出生日期", databaseFormat = "yyyyMMdd", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
private Date birthday;

}

controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cn.blue.easypoi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.blue.easypoi.bean.UserInfo;
import cn.blue.easypoi.mapper.UserInfoMapper;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
* @author Blue
* @date 2020/4/28
**/
@Controller
@RequestMapping("/user")
public class UserInfoController {
@Resource
private UserInfoMapper userInfoMapper;

@GetMapping("/export")
public List<UserInfo> findAll() {
List<UserInfo> list = userInfoMapper.findAll(null);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户表","UserInfo"),
UserInfo .class, list);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D://tyky/userInfo.xls");
workbook.write(fos);
System.out.println(fos);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@GetMapping("/Import")
public List<UserInfo> add() {
ImportParams params = new ImportParams();
// 表格标题行数,默认0
params.setTitleRows(1);
// 表头行数,默认1
params.setHeadRows(1);
List<UserInfo> list = null;
try {
list = ExcelImportUtil.importExcel(
new FileInputStream("D://tyky/userInfo.xls"),UserInfo.class, params);
} catch (Exception e) {
e.printStackTrace();
}
// stream().map(item -> new SimpleFormatter(item.birthday, "yyyy-MM-dd"))
// list.forEach(System.out::println);
System.out.println(list.size());
System.out.println(ReflectionToStringBuilder.toString(list.get(0)));
return null;
}
}

下载的Excle表格

Easypoi官网文档

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  1. © 2020 Liu Yang    湘ICP备20003709号

请我喝杯咖啡吧~

支付宝
微信