EasyExcel是阿里巴巴出品的Excel文件处理工具,功能十分强大,能省去我们很多的编码,下面来通过一个例子,看看如何使用EasyExcel导出Excel文件的
准备我们从数据库读取User实体对象数据,然后使用EasyExcel写入到HttpServletResponse输出流中,在浏览器就可以下载导出的Excel文件了
User实体@ExcelProperty(value = “id”) 表示导出的Excel文件列名为id,其他同理
package com.example.excel.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class User {
@ExcelProperty("id")
private String id;
@ExcelProperty("username")
private String username;
@ExcelProperty("password")
private String password;
@ExcelProperty("address")
private String address;
@ExcelProperty("email")
private String email;
@ExcelProperty("profile")
private String profile;
@ExcelProperty("birthday")
@DateTimeFormat("yyyy-MM-dd")
private Date birthday;
@ExcelProperty("registerDay")
@DateTimeFormat("yyyy-MM-dd")
private Date registerDay;
@ExcelProperty("loginDay")
@DateTimeFormat("yyyy-MM-dd")
private Date loginDay;
@ExcelProperty("status")
private Integer status;
@ExcelProperty("account")
private BigDecimal account;
@ExcelProperty("balance")
private BigDecimal balance;
@ExcelProperty("age")
private Integer age;
@ExcelProperty("sex")
private Integer sex;
@ExcelProperty("avatar")
private String avatar;
@ExcelProperty("level")
private Integer level;
}
ExcelUtil
实现浏览器导出下载Excel文件,写入本地文件也类似,可参考:EasyExcel使用
package com.example.excel.util;
import com.alibaba.excel.EasyExcel;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
public class ExcelUtil {
public static void exportExcel(List> objects, Class clazz, HttpServletResponse response) throws IOException {
// 设置响应头
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 设置防止中文名乱码
String filename = URLEncoder.encode("user info", "utf-8");
// 文件下载方式(附件下载还是在当前浏览器打开)
response.setHeader("Content-disposition", "attachment;filename=" +
filename + ".xlsx");
// 向Excel中写入数据
EasyExcel.write(response.getOutputStream(), clazz)
.sheet("user info")
.doWrite(objects);
}
}
Controller
实现从MySQL读取数据,写入HttpServletResponse流中的主要逻辑
package com.example.excel.controller;
import com.example.excel.entity.User;
import com.example.excel.mapper.UserMapper;
import com.example.excel.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@RestController
@Slf4j
public class ExcelController {
@Resource
private UserMapper userMapper;
@GetMapping("/excel")
public void excel(HttpServletResponse response) throws IOException {
long st = System.currentTimeMillis();
List users = userMapper.selectSub(0, 10000);
ExcelUtil.exportExcel(users, User.class, response);
log.info("export excel success, cost {} s.", (System.currentTimeMillis() - st) / 1000);
}
}
效果
浏览器输入接口地址下载Excel文件
APIFox
注意:返回的是文件流
暑期编程PK赛 得CSDN机械键盘等精美礼品!


