com.alibaba
easyexcel
2.1.4
package com.jianmin.config;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import java.util.Date;
/**
* @Author: jianmin.li
* @Description: 定义学生实体类
* @Date: 2019/8/29 16:24
* @Version: 1.0
*/
@Data
@ColumnWidth(20)
public class Student {
/**
* 将姓名指定为excel的第一列,表头为value
*/
@ExcelProperty(index = 0, value = "姓名")
private String name;
/**
* 将年龄指定为excel的第二列,表头为value
*/
@ExcelProperty(index = 1, value = "年龄")
private Integer age;
/**
* 将生日指定为第三列,表头为value,日期格式指定为'dd/MM/yyyy HH:mm:ss'
*/
@ExcelProperty(index = 2, value = "生日")
@DateTimeFormat("dd/MM/yyyy HH:mm:ss")
private Date birthday;
@Override
public String toString() {
return JSON.toJSONString(this,SerializerFeature.WriteDateUseDateFormat);
}
}
2、导出excel代码示例
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
//设置响应域
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition","attachment;filename=demo.xlsx");
//单元格合并策略-->对第一列每两行进行一次合并
LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2,0);
//单元格合并策略-->对第二列每四行进行一次合并
LoopMergeStrategy loopMergeStrategy1 = new LoopMergeStrategy(4,1);
//单元格合并策略-->对第一列到第二列的第一行到第二行进行合并,所有合并策略均不合并表头
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(1,2,0,1);
//构建excelWriter
@Cleanup("finish") ExcelWriter writer = EasyExcel
//将数据映射到Student实体类并响应到浏览器
.write(new BufferedOutputStream(response.getOutputStream()),Student.class)
//自动关闭输出流
.autoCloseStream(Boolean.TRUE)
//excel版本,强烈建议导出07版excel,因为省内存
.excelType(ExcelTypeEnum.XLSX)
//是否需要表头
.needHead(Boolean.TRUE)
//注册单元格合并策略
//.registerWriteHandler(loopMergeStrategy)
//.registerWriteHandler(loopMergeStrategy1)
.registerWriteHandler(onceAbsoluteMergeStrategy)
//设置excel保护密码
.password("123456")
//构建
.build();
//初始化sheet
WriteSheet writeSheet = EasyExcel.writerSheet().build();
writeSheet.setSheetNo(1);
writeSheet.setSheetName("Sheet1");
//获取数据
List<Student> list = getStudents();
//导出excel,注意导出结束要finish
writer.write(list,writeSheet);
}
private List<Student> getStudents() {
List<Student> list = new ArrayList<>();
Student student = new Student();
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
list.add(student);
list.add(student1);
list.add(student2);
list.add(student3);
student.setAge(25);
student.setBirthday(new Date());
student.setName("王老五");
student1.setAge(26);
student1.setBirthday(new Date());
student1.setName("王老五");
student2.setAge(27);
student2.setBirthday(new Date());
student2.setName("李老六");
student3.setAge(28);
student3.setBirthday(new Date());
student3.setName("李老六");
return list;
}
3、导出结果
导出excel
package com.jianmin.config;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Author: jianmin.li
* @Description: 自定义分析事件监听
* @Date: 2019/8/29 16:29
* @Version: 1.0
*/
public class ExcelListener extends AnalysisEventListener<Student> {
private final List<Student> list = new ArrayList<>();
/**
* 一行一行的读取数据,将数据放到集合,批量处理
*
* @param student
* @param analysisContext
* @return : void
* @Author: jianmin.li
* @Date: 2019/10/23 9:59
*/
@Override
public void invoke(Student student,AnalysisContext analysisContext) {
list.add(student);
if (list.size() == 2) {
doSomething();
}
}
private void doSomething() {
for (Student student : list) {
System.err.println(student);
}
list.clear();
}
/**
* 数据读取完进行的操作
*
* @param analysisContext
* @return : void
* @Author: jianmin.li
* @Date: 2019/10/23 10:05
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
if (list.size() > 0) {
doSomething();
}
}
/**
* 获取表头信息
*
* @param headMap
* @param context
* @return : void
* @Author: jianmin.li
* @Date: 2019/10/23 10:06
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap,AnalysisContext context) {
System.err.println(JSON.toJSONString(headMap));
Integer sheetNo = context.readSheetHolder().getReadSheet().getSheetNo();
System.err.println(sheetNo);
}
}
2、excel导入示例
@PostMapping("/upload")
public void upload(MultipartFile file) throws IOException {
EasyExcel
//将数据映射到Student实体类,并由自定义的分析事件监听处理数据
.read(new BufferedInputStream(file.getInputStream()),Student.class,new ExcelListener())
//表头最大行数
.headRowNumber(1)
//是否对单元格内容自动去除两边的空格
.autoTrim(Boolean.TRUE)
//是否自动关闭输入流
.autoCloseStream(Boolean.TRUE)
//excel保护密码
.password("123456")
//开始导入
.doReadAll();
}
3、导入结果
excel导入