package com.chen.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.chen.model.UserData;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.time.LocalDateTime;
@Controller
public class ExportController {
@GetMapping("/")
public String index() {
return "export";
}
@GetMapping("/export")
public void export(HttpServletResponse response) throws Exception {
// 模板路径
ClassPathResource resource = new ClassPathResource("/templates/template.xlsx");
InputStream templateInputStream = resource.getInputStream();
String templateFileName = "D:/my_code/java_code/simpleDemo/EasyExcel/src/main/resources/templates/template.xlsx";
// 设置响应信息
// 设置文件类型 让浏览器知道是 Excel 文件
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// 设置编码格式 防止中文乱码
response.setCharacterEncoding("utf-8");
// 编码文件名 避免中文/空格乱码
String fileName = URLEncoder.encode("用户信息导出", "UTF-8").replaceAll("\\+", "%20");
// 设置下载响应头 通知浏览器“这个是个下载文件”,并给出文件名
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
boolean isEnd = false;
try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), UserData.class).withTemplate(templateFileName).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 可以将数据进行分批次导入
while(!isEnd){
//每次从数据库里获取一批数据,判断是不是最后一批数据
List dataList = Arrays.asList(
new UserData("张三", 25, "[email protected] ",LocalDateTime.of(2002,12,22,12,34,56)),
new UserData("李四", 30, "[email protected] ",LocalDateTime.of(2002,12,22,12,34,56)),
new UserData("王五", 28, "[email protected] ",LocalDateTime.of(2002,12,22,12,34,56))
);
List dataList2 = Arrays.asList(
new UserData("张三", 25, "[email protected] ",LocalDateTime.of(2002,12,22,12,34,56)),
new UserData("李四", 30, "[email protected] ",LocalDateTime.of(2002,12,22,12,34,56)),
new UserData("王五", 28, "[email protected] ",LocalDateTime.of(2002,12,22,12,34,56))
);
excelWriter.fill(dataList,writeSheet);
excelWriter.fill(dataList2,writeSheet);
//如果是的话就更该isEnd
isEnd = true;
//这样就可以一批一批的进行数据导出,防止大文件的时候内存溢出
}
// // 写入list之前的数据
Map map = new HashMap();
map.put("curdate", "2019年10月9日13:28:28");
excelWriter.fill(map, writeSheet);
//
// // list 后面还有个统计 想办法手动写入
// // 这里偷懒直接用list 也可以用对象
List> totalListList = ListUtils.newArrayList();
List totalList = ListUtils.newArrayList();
totalListList.add(totalList);
totalList.add(null);
totalList.add(null);
totalList.add(null);
// 第四列
totalList.add("统计:1000");
// 这里是write 别和fill 搞错了
excelWriter.write(totalListList, writeSheet);
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
}
}
}
package com.chen.model;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.Date;
public class UserData {
private String name;
private Integer age;
private String email;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
private LocalDateTime date;
public UserData(String name, Integer age, String email,LocalDateTime date) {
this.name = name;
this.age = age;
this.email = email;
this.date = date;
}
// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public LocalDateTime getDate() {
return date;
}
public void setDate(LocalDateTime date) {
this.date = date;
}
}
Excel 导入导出
用户信息导出
导出 Excel
用户信息导入