阿里出品用来处理Excel的开源工具
为什么要使用 easyexcel ?
https://github.com/alibaba/easyexcel
官网文档:
https://alibaba-easyexcel.github.io/
阿里 EasyExcel 7 行代码优雅地实现 Excel 文件生成&下载功能
https://segmentfault.com/a/1190000019472781#articleHeader13
https://mvnrepository.com/artifact/com.alibaba/easyexcel
所以本文demo使用2.0.5版本
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.5</version>
</dependency>
https://github.com/qidasheng2012/springboot2.x_ssm/tree/branche-excel
注:本项目在SpringBoot2.x搭建SSM项目基础上进行开发
【初始化sql脚本】
执行脚本,创建初始数据
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '姓名',
`age` int(3) DEFAULT NULL COMMENT '年龄',
`birthday` date DEFAULT NULL COMMENT '出生日期',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_unicode_ci;
INSERT INTO `user`
VALUES ('1', '张三', '23', '1996-10-23');
INSERT INTO `user`
VALUES ('2', '李四', '24', '1995-10-30');
【实体类 User】
package com.springboot.ssm.domain;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;
import java.util.Date;
@Data
public class User {
// 主键id
@ExcelIgnore // 生成报表时忽略,不生成次字段
private Integer id;
@ExcelProperty(value = "姓名", index = 0) // 定义表头名称和位置,0代表第一列
private String name;
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
@ColumnWidth(20) // 定义列宽
@DateTimeFormat(value = "yyyy/MM/dd")
@ExcelProperty(value = "出生日期", index = 2)
private Date birthday;
}
【工具类】
package com.springboot.ssm.util;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
/**
* Excel工具类
*/
@Slf4j
public class ExcelUtils {
/**
* 导出Excel(07版.xlsx)到指定路径下
*
* @param path 路径
* @param excelName Excel名称
* @param sheetName sheet页名称
* @param clazz Excel要转换的类型
* @param data 要导出的数据
*/
public static void export2File(String path, String excelName, String sheetName, Class clazz, List data) {
String fileName = path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue());
EasyExcel.write(fileName, clazz).sheet(sheetName).doWrite(data);
}
/**
* 导出Excel(07版.xlsx)到web
*
* @param response 响应
* @param excelName Excel名称
* @param sheetName sheet页名称
* @param clazz Excel要转换的类型
* @param data 要导出的数据
* @throws Exception
*/
public static void export2Web(HttpServletResponse response, String excelName, String sheetName, Class clazz, List data) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
excelName = URLEncoder.encode(excelName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
}
/**
* 将指定位置指定名称的Excel导出到web
*
* @param response 响应
* @param path 文件路径
* @param excelName 文件名称
* @return
* @throws UnsupportedEncodingException
*/
public static String export2Web4File(HttpServletResponse response, String path, String excelName) throws UnsupportedEncodingException {
File file = new File(path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue()));
if (!file.exists()) {
return "文件不存在!";
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
excelName = URLEncoder.encode(excelName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
try (
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
) {
IOUtils.copy(in, out);
return "导出成功!";
} catch (Exception e) {
log.error("导出文件异常:", e);
}
return "导出失败!";
}
}
【调用】
package com.springboot.ssm.controller;
import com.alibaba.excel.EasyExcel;
import com.springboot.ssm.annotation.LogAnnotation;
import com.springboot.ssm.domain.User;
import com.springboot.ssm.listener.UserDataListener;
import com.springboot.ssm.service.UserService;
import com.springboot.ssm.util.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
// 在application.yml中配置
@Value("${excel.path}")
private String path;
@Autowired
private UserService userService;
@RequestMapping("/toUserListPage")
public String toUserListPage() {
return "user/userList";
}
@LogAnnotation(description = "获取所有用户信息", clazz = User.class)
@RequestMapping("/getAll")
@ResponseBody
public List<User> getAll(User user) {
return userService.getAll();
}
// easyexcel导出Excel到指定位置
@GetMapping("/export2File")
@ResponseBody
public String export2File() {
ExcelUtils.export2File(path, "用户表", "用户信息", User.class, userService.getAll());
return "导出成功";
}
// easyexcel导出Excel到web
@GetMapping("/export2Web")
public void export2Web(HttpServletResponse response) {
try {
ExcelUtils.export2Web(response, "用户表", "用户信息", User.class, userService.getAll());
} catch (Exception e) {
log.error("报表导出异常:", e);
}
}
// 将指定位置指定名称的Excel导出到web
@GetMapping("/export2Web4File/{excelName}")
@ResponseBody
public String export2Web4File(HttpServletResponse response, @PathVariable String excelName) {
try {
return ExcelUtils.export2Web4File(response, path, excelName);
} catch (Exception e) {
log.error("文件导出异常:", e);
}
return "文件导出失败";
}
// easyexcel读取文件
@GetMapping("/read4File")
@ResponseBody
public String read4File() {
String fileName = path + "用户表导入.xlsx";
EasyExcel.read(fileName, User.class, new UserDataListener(userService)).sheet().doRead();
return "读取成功";
}
// 跳转到上传页面
@RequestMapping("/toUploadPage")
public String toUploadPage() {
return "user/upload";
}
// easyexcel上传文件
@PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(), User.class, new UserDataListener(userService)).sheet().doRead();
return "上传成功";
}
}
【UserDataListener】
package com.springboot.ssm.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.springboot.ssm.domain.User;
import com.springboot.ssm.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class UserDataListener extends AnalysisEventListener<User> {
private UserService userService;
public UserDataListener(UserService userService) {
this.userService = userService;
}
/**
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<User> list = new ArrayList<User>();
@Override
public void invoke(User data, AnalysisContext context) {
log.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data);
if (list.size() >= BATCH_COUNT) {
saveData();
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
log.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveData() {
log.info("{}条数据,开始存储数据库!", list.size());
if (!CollectionUtils.isEmpty(list)) {
userService.saveBatch(list);
}
log.info("存储数据库成功!");
}
}
【1】导出Excel到指定位置
http://localhost:8080/user/export2File
【2】导出Excel到web
http://localhost:8080/user/export2Web
【3】将指定位置指定名称的Excel导出到web
http://localhost:8080/user/export2Web4File/{excelName}
例: http://localhost:8080/user/export2Web4File/用户表导入
【4】easyexcel读取文件
http://localhost:8080/user/read4File
处理逻辑是将Excel中的数据保存到数据库中
【5】easyexcel上传文件
http://localhost:8080/user/toUploadPage
导入前数据库数据
选择要导入的文件
点击“提交”,把数据保存到数据库
OK,大功告成
原网址: 访问
创建于: 2021-03-17 10:09:10
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论