Skip to content

Commit 6bd907e

Browse files
committed
springboot-easypoi
1 parent 307cdb2 commit 6bd907e

File tree

15 files changed

+445
-3
lines changed

15 files changed

+445
-3
lines changed

springboot-easypoi/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-easypoi/pom.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-easypoi</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-easypoi</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.mybatis.spring.boot</groupId>
28+
<artifactId>mybatis-spring-boot-starter</artifactId>
29+
<version>2.0.1</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<optional>true</optional>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<!-- easypoi -->
49+
<dependency>
50+
<groupId>cn.afterturn</groupId>
51+
<artifactId>easypoi-base</artifactId>
52+
<version>3.2.0</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>cn.afterturn</groupId>
56+
<artifactId>easypoi-web</artifactId>
57+
<version>3.2.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>cn.afterturn</groupId>
61+
<artifactId>easypoi-annotation</artifactId>
62+
<version>3.2.0</version>
63+
</dependency>
64+
<!--<dependency> 这个相当于上面三个,用这个会启动报错,貌似是jar包冲突
65+
<groupId>cn.afterturn</groupId>
66+
<artifactId>easypoi-spring-boot-starter</artifactId>
67+
<version>3.2.0</version>
68+
</dependency>-->
69+
70+
<!-- 通用Mapper启动器 -->
71+
<dependency>
72+
<groupId>tk.mybatis</groupId>
73+
<artifactId>mapper-spring-boot-starter</artifactId>
74+
<version>2.1.0</version>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
80+
</dependency>
81+
</dependencies>
82+
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-maven-plugin</artifactId>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
92+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import tk.mybatis.spring.annotation.MapperScan;
6+
7+
@SpringBootApplication
8+
@MapperScan("cn.tellsea.mapper")
9+
public class SpringbootEasypoiApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringbootEasypoiApplication.class, args);
13+
}
14+
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.tellsea.bean;
2+
3+
import cn.afterturn.easypoi.excel.annotation.Excel;
4+
import lombok.Data;
5+
6+
import javax.persistence.Table;
7+
import javax.validation.constraints.NotBlank;
8+
import java.io.Serializable;
9+
import java.util.Date;
10+
11+
@Data
12+
@Table(name = "tb_user")
13+
public class User implements Serializable {
14+
15+
@Excel(name = "id", width = 15)
16+
@NotBlank(message = "该字段不能为空")
17+
private Integer id;
18+
19+
@Excel(name = "姓名", orderNum = "0", width = 30)
20+
private String name;
21+
22+
@Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1", width = 30)
23+
private String sex;
24+
25+
@Excel(name = "生日", exportFormat = "yyyy-MM-dd HH:mm:ss", importFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "2", width = 30)
26+
private Date birthday;
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cn.tellsea.mapper;
2+
3+
import cn.tellsea.bean.User;
4+
import tk.mybatis.mapper.common.Mapper;
5+
import tk.mybatis.mapper.common.MySqlMapper;
6+
7+
public interface UserMapper extends Mapper<User>, MySqlMapper<User> {
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.tellsea.service;
2+
3+
import cn.tellsea.bean.User;
4+
5+
import java.util.List;
6+
7+
public interface UserService {
8+
9+
List<User> selectAll();
10+
11+
void insertList(List<User> list);
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.tellsea.service.impl;
2+
3+
import cn.tellsea.bean.User;
4+
import cn.tellsea.mapper.UserMapper;
5+
import cn.tellsea.service.UserService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class UserServiceImpl implements UserService {
13+
14+
@Autowired
15+
private UserMapper userMapper;
16+
17+
@Override
18+
public List<User> selectAll() {
19+
return userMapper.selectAll();
20+
}
21+
22+
@Override
23+
public void insertList(List<User> list) {
24+
userMapper.insertList(list);
25+
}
26+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package cn.tellsea.utils;
2+
3+
import cn.afterturn.easypoi.excel.ExcelExportUtil;
4+
import cn.afterturn.easypoi.excel.ExcelImportUtil;
5+
import cn.afterturn.easypoi.excel.entity.ExportParams;
6+
import cn.afterturn.easypoi.excel.entity.ImportParams;
7+
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.apache.poi.ss.usermodel.Workbook;
10+
import org.springframework.web.multipart.MultipartFile;
11+
12+
import javax.servlet.http.HttpServletResponse;
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.net.URLEncoder;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.NoSuchElementException;
19+
20+
//Excel导入导出工具类
21+
public class ExcelUtils {
22+
23+
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
24+
boolean isCreateHeader, HttpServletResponse response) {
25+
ExportParams exportParams = new ExportParams(title, sheetName);
26+
exportParams.setCreateHeadRows(isCreateHeader);
27+
defaultExport(list, pojoClass, fileName, response, exportParams);
28+
}
29+
30+
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
31+
HttpServletResponse response) {
32+
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
33+
}
34+
35+
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
36+
defaultExport(list, fileName, response);
37+
}
38+
39+
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
40+
ExportParams exportParams) {
41+
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
42+
if (workbook != null)
43+
;
44+
downLoadExcel(fileName, response, workbook);
45+
}
46+
47+
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
48+
try {
49+
response.setCharacterEncoding("UTF-8");
50+
response.setHeader("content-Type", "application/vnd.ms-excel");
51+
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
52+
workbook.write(response.getOutputStream());
53+
} catch (IOException e) {
54+
// throw new NormalException(e.getMessage());
55+
}
56+
}
57+
58+
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
59+
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
60+
if (workbook != null)
61+
;
62+
downLoadExcel(fileName, response, workbook);
63+
}
64+
65+
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
66+
if (StringUtils.isBlank(filePath)) {
67+
return null;
68+
}
69+
ImportParams params = new ImportParams();
70+
params.setTitleRows(titleRows);
71+
params.setHeadRows(headerRows);
72+
List<T> list = null;
73+
try {
74+
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
75+
} catch (NoSuchElementException e) {
76+
// throw new NormalException("模板不能为空");
77+
} catch (Exception e) {
78+
e.printStackTrace();
79+
// throw new NormalException(e.getMessage());
80+
}
81+
return list;
82+
}
83+
84+
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
85+
Class<T> pojoClass) {
86+
if (file == null) {
87+
return null;
88+
}
89+
ImportParams params = new ImportParams();
90+
params.setTitleRows(titleRows);
91+
params.setHeadRows(headerRows);
92+
List<T> list = null;
93+
try {
94+
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
95+
} catch (NoSuchElementException e) {
96+
// throw new NormalException("excel文件不能为空");
97+
} catch (Exception e) {
98+
// throw new NormalException(e.getMessage());
99+
System.out.println(e.getMessage());
100+
}
101+
return list;
102+
}
103+
104+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.tellsea.web;
2+
3+
import cn.afterturn.easypoi.excel.ExcelImportUtil;
4+
import cn.afterturn.easypoi.excel.entity.ImportParams;
5+
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
6+
import cn.tellsea.bean.User;
7+
import cn.tellsea.service.UserService;
8+
import cn.tellsea.utils.ExcelUtils;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.web.bind.annotation.*;
12+
import org.springframework.web.multipart.MultipartFile;
13+
14+
import javax.servlet.http.HttpServletResponse;
15+
import java.io.IOException;
16+
import java.util.List;
17+
18+
/**
19+
* 数据导出导入功能
20+
* easypoi官方文档:http://easypoi.mydoc.io/
21+
* 修改自定义样式:https://blog.csdn.net/qq_37598011/article/details/80918565
22+
*/
23+
@Slf4j
24+
@RestController
25+
public class ExcelController {
26+
27+
@Autowired
28+
private UserService userService;
29+
30+
/**
31+
* 导出Excel
32+
*
33+
* @param response
34+
*/
35+
@GetMapping("/exportExcel")
36+
public void exportExcel(HttpServletResponse response) {
37+
List<User> personList = userService.selectAll();
38+
// 导出操作
39+
ExcelUtils.exportExcel(personList, "easypoi导出功能", "导出sheet1", User.class, "测试user.xls", response);
40+
}
41+
42+
/**
43+
* 导入Excel
44+
*
45+
* @param file
46+
* @return
47+
*/
48+
@PostMapping("/importExcel")
49+
public String importExcel(@RequestParam("file") MultipartFile file) {
50+
ImportParams importParams = new ImportParams();
51+
// 数据处理
52+
importParams.setHeadRows(1);
53+
importParams.setTitleRows(1);
54+
// 需要验证
55+
importParams.setNeedVerfiy(false);
56+
try {
57+
ExcelImportResult<User> result = ExcelImportUtil.importExcelMore(file.getInputStream(), User.class, importParams);
58+
List<User> userList = result.getList();
59+
userService.insertList(userList);
60+
log.info("从Excel导入数据一共 {} 行 ", userList.size());
61+
return "导入成功";
62+
} catch (IOException e) {
63+
log.error("导入失败:{}", e.getMessage());
64+
return "导入失败";
65+
} catch (Exception e1) {
66+
log.error("导入失败:{}", e1.getMessage());
67+
return "导入失败";
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)