easyPoi导出多sheet页 一个班级一张Sheet

需求:

  1. 某学校三年级共有三个班:三年一班、三年二班、三年三班
  2. 每个班有N个人

以班级为单位,导出所有人的名单,导出到一个Excel文件中,分不同的Sheet页

数据库表

数据

结果

Controller层

java 复制代码
@GetMapping("exportMultiSheetClassWithStudents")
    public void exportMultiSheetClassWithStudents(HttpServletResponse response){

        List<Clazz> clazzList = clazzService.list();
        List<Long> clazzIds = clazzList.stream().map(e -> e.getClassId()).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(clazzIds)){

             //用in一次查出,减少数据库查询次数
             LambdaQueryWrapper<Student> studentLambdaQueryWrapper =
                     new LambdaQueryWrapper<Student>().in(CollectionUtil.isNotEmpty(clazzList),Student::getClassId,clazzIds);
             List<Student> students = studentService.list(studentLambdaQueryWrapper);

             //Student->转StudentMultiSheetExportEntity
            List<StudentMultiSheetExportEntity> studentMultiSheetExportEntities = students.stream().map(e -> {

                StudentMultiSheetExportEntity studentMultiSheetExportEntity = new StudentMultiSheetExportEntity();
                BeanUtil.copyProperties(e, studentMultiSheetExportEntity);
                return studentMultiSheetExportEntity;

            }).collect(Collectors.toList());

            //根据班级分组
            Map<Long, List<StudentMultiSheetExportEntity>> studentMultiSheetMap = studentMultiSheetExportEntities.stream().collect(Collectors.groupingBy(StudentMultiSheetExportEntity::getClassId));

            // 将sheet1和sheet2使用得map进行包装
            List<Map<String, Object>> sheetsList = new ArrayList<>();
            for (Map.Entry<Long, List<StudentMultiSheetExportEntity>> studentListEntry : studentMultiSheetMap.entrySet()) {

                // 创建参数对象
                ExportParams exportParams = new ExportParams();
                String clazzName = studentListEntry.getKey()+"班";
                // 设置sheet得名称
                exportParams.setSheetName(clazzName);

                // 创建sheet使用得map
                Map<String,Object> dataMap = new HashMap<>(4);
                // title的参数为ExportParams类型
                dataMap.put("title",exportParams);
                // 对应的实体类型必须是带@Excel注解的实体不是数据库实体,用数据库实体会报错
                dataMap.put("entity", StudentMultiSheetExportEntity.class);
                // sheet中要填充得数据
                dataMap.put("data",studentListEntry.getValue());

                sheetsList.add(dataMap);
            }

            Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);

            ExcelUtil.downLoadExcel("学生信息"+ System.currentTimeMillis() +".xls",response,workbook);

        }


    }

student实体

java 复制代码
@Data
@EqualsAndHashCode(callSuper = false)
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    //这里要写,否则会报错
    @TableId(type = IdType.AUTO)
    private Long id;

    private String name;

    private Integer sex;

    private LocalDateTime birthDay;

    private LocalDateTime registrationDate;

    private Long classId;


}

StudentMultiSheetExportEntity实体

java 复制代码
@Data
public class StudentMultiSheetExportEntity implements java.io.Serializable{

    /**
     * id
     */
    private Long id;
    /**
     * 学生姓名
     */
    @Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
    private String        name;
    /**
     * 学生性别
     */
    @Excel(name = "学生性别", replace = { "男_1", "女_0" }, suffix = "生", isImportField = "true_st")
    private int           sex;

    @Excel(name = "出生日期", databaseFormat = "yyyy-MM-dd HH:mm:ss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
    private LocalDateTime birthDay;

    @Excel(name = "进校日期", databaseFormat = "yyyy-MM-dd HH:mm:ss", format = "yyyy-MM-dd")
    private LocalDateTime registrationDate;

    @ExcelIgnore
    private Long classId;

}

ExcelUtil

java 复制代码
public class ExcelUtil {

    public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            //设置浏览器响应头对应的Content-disposition
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            //编码
            response.setCharacterEncoding("UTF-8");
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
相关推荐
不坑老师1 天前
小工具显出大才能——不坑盒子为教育数字化转型贡献“新方案”
microsoft·word·excel·ppt·office
骆驼爱记录1 天前
Python程序打包全攻略
自动化·word·excel·wps·新人首发
GHL2842710901 天前
用lingma合并俩个excel
ai·excel
小赖同学啊1 天前
xmind用例通过excel整理方式(注意!!不是通过python解析ximind文件转化成用例)
开发语言·python·excel
wangkeyen2 天前
如何用excel拟合两元一次函数?
excel
哈哈你是真的厉害2 天前
React Native 鸿蒙跨平台开发:实现Excel数据表格
react native·excel·harmonyos
Eiceblue2 天前
【.NET 开发】通过 C# 实现 Excel 转 JSON
c#·.net·excel
fs哆哆2 天前
在VB.NET和VBA教程-操作Excel单元格的三个核心问题
ui·.net·excel
hhzz2 天前
EasyPoi的核心映射工具:@Excel注解详解
java·服务器·excel·springboot·easypoi
小妖6662 天前
excel 本地sheet往服务器上粘贴时,表格宽度没有粘过来
excel