EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>3.1.3version>
dependency>
CREATE TABLE `student` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '学生表ID',
`sname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '学生姓名',
`sno` bigint NOT NULL COMMENT '学号',
`sex` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '性别',
`age` int NOT NULL COMMENT '年龄',
`is_deleted` tinyint UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
);
CREATE TABLE `course` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '课程表ID',
`cname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '课程名',
`cno` bigint NOT NULL COMMENT '课程号',
`tno` bigint NOT NULL COMMENT '任教教师编码',
`is_deleted` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
);
CREATE TABLE `teacher` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '教师表ID',
`tname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '教师姓名',
`tno` bigint NOT NULL COMMENT '教师编号',
`sex` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '性别',
`age` int NOT NULL COMMENT '年龄',
`is_deleted` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
);
@Data
public class StudentVo {
/**学生姓名**/
@ExcelProperty(value = "姓名",index = 0)
private String sname;
/**学号**/
@ExcelProperty(value = "学号",index = 1)
private Long sno;
/**性别**/
@ExcelProperty(value = "性别",index = 2)
private String sex;
/**年龄**/
@ExcelProperty(value = "年龄",index = 3)
private Integer age;
}
@Data
public class CourseVo {
/**课程名**/
@ExcelProperty(value = "课程名",index = 0)
private String cname;
/**课程号**/
@ExcelProperty(value = "课程号",index = 1)
private Long cno;
/**任教教师编号**/
@ExcelProperty(value = "任教教师编号",index = 2)
private Long tno;
}
@Data
public class TeacherVo {
/**教师姓名**/
@ExcelProperty(value = "姓名", index = 0)
private String tname;
/**教师编号**/
@ExcelProperty(value = "教师编号", index = 1)
private Long tno;
/**性别**/
@ExcelProperty(value = "性别", index = 2)
private String sex;
/**年龄**/
@ExcelProperty(value = "年龄", index = 3)
private Integer age;
}
@RestController
@RequestMapping("/easyExcel")
public class EasyExcelController {
@Resource
private IEasyExcelService easyExcelService;
@PostMapping("excelExport")
public void excelExport(HttpServletResponse response){
easyExcelService.excelExport(response);
}
}
public interface IEasyExcelService {
void excelExport(HttpServletResponse response);
}
public interface IStudentService extends IService<Student> {
}
public interface ICourseService extends IService<Course> {
}
public interface ITeacherService extends IService<Teacher> {
}
@Service
public class EasyExcelServiceImpl implements IEasyExcelService {
@Resource
private StudentMapper studentMapper;
@Resource
private CourseMapper courseMapper;
@Resource
private TeacherMapper teacherMapper;
@Override
public void excelExport(HttpServletResponse response) {
try {
String fileName = "student";
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
WriteSheet studentInfo = EasyExcel.writerSheet(0,"student")
.head(StudentVo.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.build();
WriteSheet courseInfo = EasyExcel.writerSheet(1,"course")
.head(CourseVo.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.build();
WriteSheet teacherInfo = EasyExcel.writerSheet(2,"teacher")
.head(TeacherVo.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.build();
List<Student> studentList = studentMapper.selectList(null);
List<Course> courseList = courseMapper.selectList(null);
List<Teacher> teacherList = teacherMapper.selectList(null);
excelWriter.write(studentList,studentInfo);
excelWriter.write(courseList,courseInfo);
excelWriter.write(teacherList,teacherInfo);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
excelWriter.finish();
}catch (IOException e){
e.printStackTrace();
}
}
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
}
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
}
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {
}
public interface StudentMapper extends BaseMapper<Student> {
}
public interface CourseMapper extends BaseMapper<Course> {
}
public interface TeacherMapper extends BaseMapper<Teacher> {
}
使用postman测试,测试方法如下
下载完成后,打开Excel表查看内容
操作完成