首页 > 数据库 >数据库导出到Excel, 使用mybatis分批导出,防止oom

数据库导出到Excel, 使用mybatis分批导出,防止oom

时间:2024-07-25 16:53:14浏览次数:7  
标签:row0 oom 导出 Excel createCell rowData legalContractBean setCellValue row

DAO层:

@Select("<script>" +
           " select * from legal_contract_tb where 1=1 " +
           " <if test='legalContractBean.contractName!=null and legalContractBean.contractName !=\"\"'> " +
           " and contract_name like CONCAT('%',#{legalContractBean.contractName},'%') " +
           " </if> " +
           " <if test='legalContractBean.handleStaffCode!=null and legalContractBean.handleStaffCode >0'> " +
           " and handle_staff_code =#{legalContractBean.handleStaffCode}" +
           " </if> " +
           " <if test='legalContractBean.applyStaffCode!=null and legalContractBean.applyStaffCode >0'> " +
           " and apply_staff_code =#{legalContractBean.applyStaffCode}" +
           " </if> " +
           " <if test='legalContractBean.applyAgencyCode!=null and legalContractBean.applyAgencyCode >0'> " +
           " and apply_agency_code =#{legalContractBean.applyAgencyCode}" +
           " </if> " +
           " <if test='legalContractBean.contractNumber!=null and legalContractBean.contractNumber !=\"\"'> " +
           " and contract_number like CONCAT('%',#{legalContractBean.contractNumber},'%') " +
           " </if> " +
           " <if test='legalContractBean.contractTypeCode!=null and legalContractBean.contractTypeCode>0'> " +
           " and contract_type_code =#{legalContractBean.contractTypeCode} " +
           " </if> " +
           " <if test='legalContractBean.contractSecondTypeCode!=null and legalContractBean.contractSecondTypeCode>0'> " +
           " and contract_second_type_code =#{legalContractBean.contractSecondTypeCode} " +
           " </if> " +
           " <if test='legalContractBean.operateType!=null and legalContractBean.operateType!=\"\"'> " +
           " and operate_type =#{legalContractBean.operateType} " +
           " </if> " +
           " <if test='legalContractBean.otherCompany!=null and legalContractBean.otherCompany!=\"\"'> " +
           " and other_company like CONCAT('%',#{legalContractBean.otherCompany},'%') " +
           " </if> " +
           " <if test='legalContractBean.fullOurCompany!=null and legalContractBean.fullOurCompany!=\"\"'> " +
           " and full_our_company like CONCAT('%',#{legalContractBean.fullOurCompany},'%') " +
           " </if> " +
           " <if test='legalContractBean.contractStatus!=null and legalContractBean.contractStatus!=\"\"'>" +
           "      and contract_status =#{legalContractBean.contractStatus} " +
           " </if> " +
           " <if test='legalContractBean.workflowStatus!=null and legalContractBean.workflowStatus!=\"\"'>" +
           "      and workflow_status =#{legalContractBean.workflowStatus} " +
           " </if> " +

           " <if test='startDay!=null and startDay!=\"\"'>" +
           "      and DATE_FORMAT(create_time, '%Y-%m-%d') >=#{startDay} " +
           " </if> " +
           " <if test='endDay!=null and endDay!=\"\"'>" +
           "      and DATE_FORMAT(create_time, '%Y-%m-%d') <=#{endDay} " +
           " </if> " +
           " <if test='legalContractBean.archiveStatus!=null and legalContractBean.archiveStatus!=\"\" '>" +
           "      and archive_status =#{legalContractBean.archiveStatus} " +
           " </if>" +
           " <if test='legalContractBean.description!=null and legalContractBean.description!=\"\" '>" +
           "      and description like CONCAT('%',#{legalContractBean.description},'%') " +
           " </if>" +
           " order by field (contract_status,'inProcess','expiringSoon','expire','cancel'),create_time desc" +
           "</script>")
   @Options(fetchSize = 5000, resultSetType = ResultSetType.FORWARD_ONLY)
   @ResultType(LegalContractBean.class)
   public void findAll(@Param("legalContractBean") LegalContractBean legalContractBean,
                       @Param("startDay") String startDay, @Param("endDay") String endDay,
                       ResultHandler<LegalContractBean> handler);

SERVICE层:

public SXSSFWorkbook exportList(LegalContractBean legalContractBean, String startDay,String endDay){
        List<LegalContractBean> list = new ArrayList<>();
        legalContractDao.findAll(legalContractBean,startDay,endDay, new ResultHandler<LegalContractBean>() {
            @Override
            public void handleResult(ResultContext<? extends LegalContractBean> resultContext) {
                LegalContractBean resultObject = resultContext.getResultObject();
                list.add(resultObject);
            }
        });
        for(LegalContractBean excelBean : list){
            List<LegalContractSigningBean> signingList = legalContractSigningDao.findByContractCode(excelBean.getCode(), null);
            //计算签约信息金额总和
            BigDecimal totalAmount = signingList.stream().filter(signing->signing.getPayTotal()!=null)
                    .map(LegalContractSigningBean::getPayTotal)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);;
            excelBean.setTotalAmount(totalAmount.toString());
        }
        return assemblyData(list);
    }
public SXSSFWorkbook assemblyData(List<LegalContractBean> list) {
        SXSSFWorkbook wb = new SXSSFWorkbook(100);
        SXSSFSheet sheet = wb.createSheet("Sheet1");
        SXSSFRow row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("合同子分类");
        row0.createCell(1).setCellValue("合同名称");
        row0.createCell(2).setCellValue("合同分类");
        row0.createCell(3).setCellValue("经办人");
        row0.createCell(4).setCellValue("申请人");
        row0.createCell(5).setCellValue("申请部门");
        row0.createCell(6).setCellValue("创建时间");
        row0.createCell(7).setCellValue("我方公司");
        row0.createCell(8).setCellValue("对方公司");
        row0.createCell(9).setCellValue("有效期");
        row0.createCell(10).setCellValue("合同编号");
        row0.createCell(11).setCellValue("合同金额");
        row0.createCell(12).setCellValue("归属项目");
        row0.createCell(13).setCellValue("流程状态");
        for (int i = 0; i < list.size(); i++) {
            LegalContractBean rowData = list.get(i);
            SXSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(rowData.getContractSecondType());
            row.createCell(1).setCellValue(rowData.getContractName());
            row.createCell(2).setCellValue(rowData.getContractType());
            row.createCell(3).setCellValue(rowData.getHandleStaffName());
            row.createCell(4).setCellValue(rowData.getApplyStaffName());
            row.createCell(5).setCellValue(rowData.getApplyAgencyName());
            String createTime = DateUtils.dateToString(rowData.getCreateTime());
            row.createCell(6).setCellValue(createTime);
            row.createCell(7).setCellValue(rowData.getOurCompany());
            row.createCell(8).setCellValue(rowData.getOtherCompany());
            row.createCell(9).setCellValue(getValidityTerm(rowData));
            row.createCell(10).setCellValue(rowData.getContractNumber());
            row.createCell(11).setCellValue(rowData.getTotalAmount());
            String belongProject = getBelongProject(rowData.getBelongProject());
            row.createCell(12).setCellValue(belongProject);
            String workflowStatusCh = ContractEnums.WorkFlowStatus.description(rowData.getWorkflowStatus());
            row.createCell(13).setCellValue(workflowStatusCh);
        }
        return wb;
    }

CONTROLLER层:

    @PassOaAuth
    @RequestMapping("/export/list")
    public ResponseEntity<Object> exportList(LegalContractBean legalContractBean, String startDay, String endDay){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try(SXSSFWorkbook workbook = legalContractService.exportList(legalContractBean, startDay, endDay)) {
            workbook.write(out);
            String exportFileName ="合同列表导出.xlsx";
            String fileNameUrl = URLEncoder.encode(exportFileName, "UTF-8");
            return ResponseEntity.ok().header("Content-disposition", "attachment;filename="+fileNameUrl+";"+"filename*=utf-8''"+fileNameUrl).
                    contentType(MediaType.parseMediaType("application/vnd.ms-excel")).
                    contentLength(out.size()).
                    body(out.toByteArray());
        } catch (Exception e) {
            log.info("导出数据异常,", e);
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(new ResultBean<Object>(false,"导出异常",null));
        }
    }

 

  

标签:row0,oom,导出,Excel,createCell,rowData,legalContractBean,setCellValue,row
From: https://www.cnblogs.com/sbk613/p/18323602

相关文章

  • EasyExcel 读取xls 监听行数据问题
    需求:导入xls文件需要判断是否空值,时间格式是否问题监听器classExcelListenerextendsAnalysisEventListener<RevWaterUserDocAndUserPayImportExl>{publicExcelListener(List<RevWaterUserDocAndUserPayImportExl>result){this.list=result;......
  • springboot+vue前后端分离项目:导出功能报错Request processing failed: cn.hutool.cor
    1.报错截图: 2.hutool官网,推荐引入poi-ooxml依赖 3.mvn仓库找到依赖 4.用最新版依赖 5.复制到本项目pom.xml,刷新maven 解决......
  • 需要帮助来提取此 XML 节点 - Python 中的 Excel 连接字符串
    我有一个Python程序,打开Excel(XLSX)文件,并尝试查找<connection>节点。这是connections.xml文件中的完整XML。<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?><connectionsxmlns="http://schemas.op......
  • 如何确保 Excel 与 Bloomberg 保持连接以执行电子表格自动化?
    我会尽力让这个问题尽可能清楚,因为我是一个初学者,我很难找到这个问题的解决方案。目标是运行一个自动打开excel的python代码,运行一些用于的宏Bloomberg公式然后关闭所有内容。我的python代码如下所示:importtimeimportwin32com.clientaswin32importgcde......
  • Java编程指南:高级技巧解析 - Excel单元格样式的编程设置
    最新技术资源(建议收藏)https://www.grapecity.com.cn/resources/前言在Java开发中,处理Excel文件是一项常见的任务。在处理Excel文件时,经常需要对单元格进行样式设置,以满足特定的需求和美化要求,通过使用Java中的相关库和API,我们可以轻松地操作Excel文件并设置单元格的样式。在......
  • 表情符号错误plotly kaleido静态图片导出不支持表情符号
    https://github.com/misrori/goldhandyoutube/blob/main/aranykez.ipynb在colab中,当您显示时它会起作用。但是当您导出时它不会fromgoldhandimport*tw=Tw()t=GoldHand('TSLA')p=t.plotly_last_year(tw.get_plotly_title('TSLA'))p.upd......
  • SpringBoot+@Validated实现参数验证(非空、类型、范围、格式等)-若依前后端导入Excel
    原文链接: 霸道的程序猿的博客(cnblogs.com)若依管理系统前后端分离版基于ElementUI和SpringBoot怎样实现Excel导入和导出:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108278834SpringBoot+Vue实现excel导入带格式化的时间参数(moment格式化明天日期并设置el......
  • 调用后端接口返回导出表格
    //fetch('/record/export/report',{//method:'POST',//指定请求方法为POST//headers:{//'Content-Type':'application/json',//设置请求头,指明发送的是JSON格式的数据//......
  • 如何将 Python 列表添加到 Excel 中已有值的列的末尾?
    我目前正在尝试编写一个程序,将值附加到列表中,然后将这些值添加到Excel数据表中的列中。每次运行该程序时,我都希望在同一列的末尾添加更多值。所以我不确定如何解决这个问题,而且我在网上找到的其他答案也没有取得多大成功。以下是使用openpyxl库在Python中将......
  • [POI]主管:你尽快封装一套读、写excel的方法,尽量简单
    【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)https://www.cnblogs.com/cnb-yuchen/p/18321864出自【进步*于辰的博客】方法简单,也出于个人时间考虑,就未作过多注释和说明,可谓,本文只有代码,但相信你一看便懂。目录1、读2、写最后1、读1、文件兼容类型。//......