自学内容网 自学内容网

easyexcel导出动态、静态表头数据

@Operation(summary = "导出班级任务单数据统计", description = "入参:classTaskCourseId")
    @GetMapping("/exportClassTaskCourseStatistic")
    @SneakyThrows
    public void exportClassTaskCourseStatistic(@RequestParam Long classTaskCourseId, HttpServletResponse response) {
        ClassTaskCourse classTaskCourse = classTaskCourseService.getById(classTaskCourseId);
        ClassTask classTask = classTaskService.getById(classTaskCourse.getClassTaskId());
        // 班级任务项列表
        List<ClassTaskItem> classTaskItemList = classTaskItemService.lambdaQuery()
                .eq(ClassTaskItem::getClassTaskCourseId, classTaskCourseId)
                .list();
        // 获取班级任务天+任务单列表
        List<ClassTaskItemMapVO> classTaskItemMap = getClassTaskItemMap(classTaskItemList);
        // 获取学生任务项状态
        List<StudentTaskDaysState> studentTaskDaysState = getStudentTaskDaysState(classTask.getClassInfoId(), classTaskCourseId, classTaskItemList);
        // 构建导出表头
        Map<Long, Integer> itemsLocationMap = new HashMap<>();
        List<List<String>> headers = generateExcelHeader(classTaskItemMap, itemsLocationMap);
        // 构建导出数据
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        String fileName = URLEncoder.encode("全动力作业-学生数据统计-" + DateUtil.format(new Date(), "yyyyMMddHHmmss"), StandardCharsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream())
                .head(headers)
                .sheet("全动力作业-学生数据统计-导出")
                .doWrite(generateData(studentTaskDaysState, itemsLocationMap));
    }

    /**
     * 构建excel行数据
     * @param studentTaskDaysStates 学生任务状态列表
     * @param itemsLocationMap 任务项位置map
     * @return 行数据
     */
    private List<List<Object>> generateData(List<StudentTaskDaysState> studentTaskDaysStates, Map<Long, Integer> itemsLocationMap) {
        List<List<Object>> rows = new ArrayList<>();
        studentTaskDaysStates.forEach(studentTaskDaysState -> {
            List<Object> row = new ArrayList<>(Collections.nCopies(2 + studentTaskDaysState.getTotalTaskCount(), "--"));
            row.set(0, studentTaskDaysState.getStudentName());
            row.set(1, studentTaskDaysState.getFinishedTaskCount() + "/" + studentTaskDaysState.getTotalTaskCount());
            studentTaskDaysState.getStudentTaskStates().forEach(studentTaskState -> {
                row.set(itemsLocationMap.get(studentTaskState.getClassTaskItemId()), "√");
            });
            rows.add(row);
        });
        return  rows;
    }

    /**
     * 构建表头
     * @param taskDayItems 任务天map
     * @param itemsLocationMap 任务项位置map
     * @return 表头
     */
    private List<List<String>> generateExcelHeader(List<ClassTaskItemMapVO> taskDayItems, Map<Long, Integer> itemsLocationMap) {
        List<List<String>> headers = new ArrayList<>();
        int col = 2;
        headers.add(new ArrayList<>(Collections.singletonList("姓名")));
        headers.add(new ArrayList<>(Collections.singletonList("任务统计")));
        // 生成日期和任务项表头
        for (ClassTaskItemMapVO taskDay : taskDayItems) {
            for (ClassTaskItem taskItem : taskDay.getClassTaskItems()) {
                itemsLocationMap.put(taskItem.getId(), col++);
                headers.add(new ArrayList<>(Arrays.asList(
                        DateUtil.format(taskDay.getTaskDay(), ColumnFormatter.PATTERN_DATE), // 日期
                        taskItem.getTaskName() // 任务项名称
                )));
            }
        }
        return headers;
    }

在这里插入图片描述


原文地址:https://blog.csdn.net/weixin_42096620/article/details/144370491

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!