文章目录
- EasyExcel
- 引入依赖
- 表结构
- 学生表
- 课程表
- 教师表
- 项目结构
- 下载模板实体类
- StudentVo
- CourseVo
- TeacherVo
- Controller
- Service
- EasyExcelService
- StudentService
- CourseService
- TeacherService
- ServiceImpl
- EasyExcelServiceImpl
- StudentServiceImpl
- CourseServiceImpl
- TeacherServiceImpl
- mapper
- StudentMapper
- CourseMapper
- TeacherMapper
- 启动项目
- 测试
EasyExcel
EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
引入依赖
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.3</version> </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`)
);
多sheet上传时已上传了部分数据,所以表中目前有数据
项目结构
下载模板实体类
StudentVo
@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;}
CourseVo
@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;
}
TeacherVo
@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;
}
Controller
@RestController
@RequestMapping("/easyExcel")
public class EasyExcelController {@Resourceprivate IEasyExcelService easyExcelService;@PostMapping("excelExport")public void excelExport(HttpServletResponse response){easyExcelService.excelExport(response);}
}
Service
EasyExcelService
public interface IEasyExcelService {void excelExport(HttpServletResponse response);
}
StudentService
public interface IStudentService extends IService<Student> {
}
CourseService
public interface ICourseService extends IService<Course> {
}
TeacherService
public interface ITeacherService extends IService<Teacher> {
}
ServiceImpl
EasyExcelServiceImpl
@Service
public class EasyExcelServiceImpl implements IEasyExcelService {@Resourceprivate StudentMapper studentMapper;@Resourceprivate CourseMapper courseMapper;@Resourceprivate TeacherMapper teacherMapper;@Overridepublic 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();}}
StudentServiceImpl
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
}
CourseServiceImpl
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
}
TeacherServiceImpl
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {
}
mapper
StudentMapper
public interface StudentMapper extends BaseMapper<Student> {
}
CourseMapper
public interface CourseMapper extends BaseMapper<Course> {
}
TeacherMapper
public interface TeacherMapper extends BaseMapper<Teacher> {
}
启动项目
测试
使用postman测试,测试方法如下
下载完成后,打开Excel表查看内容
操作完成