EasyExcel implements multi-sheet file export

EasyExcel

EasyExcel is a Java-based, fast and concise Excel processing tool that solves memory overflow of large files.
It allows you to quickly complete Excel's reading, writing and other functions without considering performance, memory and other factors.

Introduce dependencies

 	<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.1.3</version> 
    </dependency>

Table Structure

Student table

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`)
);

Insert image description here

Class Schedule

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`)
);

Insert image description here

teacher list

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`)
);

Insert image description here
Part of the data has been uploaded when uploading multiple sheets, so there is currently data in the table

Project structure

Insert image description here

Download template entity class

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 {
    
    

	@Resource
    private 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 {
    
    

    @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();
        }
    }

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> {
    
    
}

Startup project

Insert image description here

test

Use postman to test. The test method is as follows.
Insert image description here
Insert image description here
After downloading, open the Excel table to view the content.
Please add image description
Please add image description
Please add image description

Operation completed

Guess you like

Origin blog.csdn.net/m0_68681879/article/details/132449121