EasyExcel export (single Sheet export, multi-Sheet export, multi-file export)

EasyExcel official document - Java-based Excel processing tool | Easy Excel

1. Single Sheet Export

@ApiOperation("对话数据导出(单话题)")
    @PostMapping("downloadByChatDialogueId")
    public void downloadByChatDialogueId(@RequestParam String chatDialogueId, HttpServletResponse response) throws IOException {
        // 话题基本信息
        ChatDialogue dialogue = chatDialogueService.getById(chatDialogueId);
        // 对话数据列表
        Result<List<ChatDialogueMappingMagic>> listResult = getById(chatDialogueId);
        List<ChatDialogueMappingMagic> mappingMagicList = listResult.getData();
        List<ChatDialogueMappingExport> exportList = converter.convert(mappingMagicList, ChatDialogueMappingExport.class);

        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=chatEduExport.xlsx");
        // 使用EasyExcel进行导出
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), ChatDialogueMappingExport.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(dialogue.getTitle()).build();
        excelWriter.write(exportList, writeSheet);
        excelWriter.finish();
    }

2. Multi-Sheet export

@ApiOperation("对话数据导出(单用户)")
    @PostMapping("downloadByUserId")
    public void downloadByUserId( @RequestParam Integer userId, HttpServletResponse response) throws IOException {
        
        ChatUser chatUser = chatUserService.getById(userId);
        Assert.notNull(chatUser, "用户不存在");


        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=chatEduExport.xlsx");
        // 使用EasyExcel进行导出
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), ChatDialogueMappingExport.class).build();

        // 查出用户的所有话题
        List<ChatDialogue> dialogueList = chatDialogueService.lambdaQuery()
                .eq(ChatDialogue::getUserId, userId)
                .orderByDesc(ChatDialogue::getChatDialogueFunctionUsedId)
                .list();
        // 循环写入多个Sheet
        for (ChatDialogue dialogue : dialogueList) {
            Integer functionUsedId = dialogue.getChatDialogueFunctionUsedId();
            if (functionUsedId == null) {
                continue;
            }
            ChatDialogueFunctionUsed functionUsed = chatDialogueFunctionUsedService.getById(functionUsedId);
            if (functionUsed == null) {
                continue;
            }
            // 对话数据列表
            Result<List<ChatDialogueMappingMagic>> listResult = getById(dialogue.getId());
            List<ChatDialogueMappingMagic> mappingMagicList = listResult.getData();
            List<ChatDialogueMappingExport> exportList = converter.convert(mappingMagicList, ChatDialogueMappingExport.class);
            WriteSheet writeSheet = EasyExcel.writerSheet("(" + functionUsed.getTitle() + ")" + dialogue.getTitle()).build();
            excelWriter.write(exportList, writeSheet);
        }

        excelWriter.finish();
    }

3. Multiple file export

@ApiOperation("对话数据导出(所有用户)")
    @PostMapping("downloadAll")
    public void downloadAll(HttpServletResponse response) throws IOException {
        
        // 所有用户
        List<ChatUser> chatUsers = chatUserService.list();

        response.setContentType("application/zip");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=chatEduExports.zip");

        ServletOutputStream outputStream = response.getOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(outputStream);
        // 循环写入多个文件
        for (ChatUser chatUser : chatUsers) {

            // 查出用户的所有话题
            List<ChatDialogue> dialogueList = chatDialogueService.lambdaQuery()
                    .eq(ChatDialogue::getUserId, chatUser.getId())
                    .isNotNull(ChatDialogue::getChatDialogueFunctionUsedId)
                    .orderByDesc(ChatDialogue::getChatDialogueFunctionUsedId)
                    .list();

            if (dialogueList.size() == 0) {
                continue;
            }

            // 创建一个临时的ByteArrayOutputStream用于存储生成的Excel文件数据
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            // 使用EasyExcel进行导出
            ExcelWriter excelWriter = EasyExcel.write(byteArrayOutputStream, ChatDialogueMappingExport.class).build();
            // 循环写入多个Sheet
            for (ChatDialogue dialogue : dialogueList) {
                Integer functionUsedId = dialogue.getChatDialogueFunctionUsedId();
                if (functionUsedId == null) {
                    continue;
                }
                ChatDialogueFunctionUsed functionUsed = chatDialogueFunctionUsedService.getById(functionUsedId);
                if (functionUsed == null) {
                    continue;
                }
                // 对话数据列表
                Result<List<ChatDialogueMappingMagic>> listResult = getById(dialogue.getId());
                List<ChatDialogueMappingMagic> mappingMagicList = listResult.getData();

                List<ChatDialogueMappingExport> exportList = converter.convert(mappingMagicList, ChatDialogueMappingExport.class);
                
                WriteSheet writeSheet = EasyExcel.writerSheet(IdUtil.fastUUID()).build();
                excelWriter.write(exportList, writeSheet);
            }
            excelWriter.finish();
            // 创建文件
            zipOut.putNextEntry(new ZipEntry(chatUser.getUsername()+".xlsx"));
            zipOut.write(byteArrayOutputStream.toByteArray());
            zipOut.closeEntry();

            // 关闭临时的ByteArrayOutputStream
            byteArrayOutputStream.close();
        }
        zipOut.finish();
        zipOut.close();
        outputStream.flush();
        outputStream.close();
    }

Guess you like

Origin blog.csdn.net/wenxingchen/article/details/131550425