【excel大数据导出】

excel导出时数据量太大导致宕机

  1. 初步解决方式
  • 分页查询

  • XSSFWorkbook 换成了SXSSFWorkbook

  1. 但是excel每一页最多一百多万数据,还是解决不了问题,最后选择导出csv文件
 public static boolean exportCsv(File file, List<Map<String,Object>> list,int page) {

        boolean isSucess = false;

        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(file,true);
            osw = new OutputStreamWriter(out, "UTF-8");
            bw = new BufferedWriter(osw,1024);
            if(list.size()>0){
                if(page==0) {
                    Map<String, Object> firstMap = list.get(0);
                    for (String key : firstMap.keySet()) {
                        bw.write("\""+key + "\",");
                    }
                    bw.write("\r\n");
                }
                    for( Map<String, Object> dataMap: list ){
                        for (String key : dataMap.keySet()) {

                            bw.write(dataMap.get(key)==null?""+",":"\""+dataMap.get(key).toString().replaceAll("\"", "\"\"")+"\",");
                        }
                        bw.write("\r\n");
                    }
            }

            isSucess=true;

        } catch (Exception e) {
            log.error("生成csv文件失败",e);
            e.printStackTrace();
            isSucess = false;
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                    bw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                    osw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

csv文件导出时,需要注意以下两个字符

  • 1、英文逗号表示换单元格,所以需要需要内容添加双引号
  • 2、如果有双引号,需要将"替换为""
  1. 导出时可采用异步的方式进行,生成文件的过程中有异常或者out of memory可进行导出状态的回滚 oom采用catch error的方式进行
在这里插入代码片

@Async
@Override
public void exportDownloadFileAtLocal(String dm_id) {

    try {
     /*业务代码*/
    } catch (Exception e) {
        logger.error("捕获exception", e);
        dealFailure(dm);
    } catch (Error e) {
        logger.error("捕获error", e);
        dealFailure(dm);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值