Springboot导出百万级数据到excel

最近项目有导出数据到excel功能,于是觉得很轻松的写了下面代码

@ApiOperation(value = "万级数据导出")
    @RequestMapping(value = "/export1",method = RequestMethod.GET)
    public void exportExcel1(HttpServletResponse response, HttpServletRequest request) throws Exception{

        //接收参数
        String accounttime = request.getParameter("accounttime");

        //创建poi导出数据对象
        //SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
        //创建sheet页
        //SXSSFSheet sheet = sxssfWorkbook.createSheet("测试1");

        XSSFWorkbook sxssfWorkbook = new XSSFWorkbook();
        XSSFSheet sheet = sxssfWorkbook.createSheet("测试1");
        //创建表头
        XSSFRow headRow = sheet.createRow(0);
        //设置表头信息
        headRow.createCell(0).setCellValue("序号");
        headRow.createCell(1).setCellValue("姓名");
        headRow.createCell(2).setCellValue("年龄");
        // 遍历上面数据库查到的数据
        for(int i =1;i<=100000;i++){

            //填充数据
            XSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            //序号
            dataRow.createCell(0).setCellValue(i);
            //看你实体类在进行填充
            dataRow.createCell(1).setCellValue("NAME"+i);
            dataRow.createCell(2).setCellValue(i%3);
        }

        // 下载导出
        String filename = "导出excel表格名字";
        // 设置头信息
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        //一定要设置成xlsx格式
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
        //创建一个输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //写入数据
        sxssfWorkbook.write(outputStream);

        // 关闭
        outputStream.close();
        sxssfWorkbook.close();
    }

一开始数据量不大的情况下,这个导出没有什么问题,但是数据在达到100w以上了直接内存溢出崩掉

 

查询Apache官网看到 还有另一个POI类 SXSSFWorkbook 所以把对应的POI类修改修改代码如下:

@ApiOperation(value = "报表查询")
    @RequestMapping(value = "/fixedReport", method = RequestMethod.GET)
    public Object selectTargetValueByCodeAndId(@RequestParam long startTime , @RequestParam long endTime  , @RequestParam Integer id , @RequestParam Integer sheetId){
        return reportTemplateTagsService.selectTargetValueByCodeAndId(id ,startTime , endTime , sheetId);
    }

    @ApiOperation(value = "百万级数据导出")
    @RequestMapping(value = "/export",method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception{

        //接收参数
        String accounttime = request.getParameter("accounttime");

        //创建poi导出数据对象
        SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
        //创建sheet页
        SXSSFSheet sheet = sxssfWorkbook.createSheet("测试1");
        //创建表头
        SXSSFRow headRow = sheet.createRow(0);
        //设置表头信息
        headRow.createCell(0).setCellValue("序号");
        headRow.createCell(1).setCellValue("姓名");
        headRow.createCell(2).setCellValue("年龄");
        headRow.createCell(3).setCellValue("账号");
        headRow.createCell(4).setCellValue("金钱");
        headRow.createCell(5).setCellValue("车数量");
        headRow.createCell(6).setCellValue("老婆数量");
        headRow.createCell(7).setCellValue("房子数量");
        headRow.createCell(8).setCellValue("朋友数量");
        headRow.createCell(9).setCellValue("鞋子");
        headRow.createCell(10).setCellValue("房号");
        headRow.createCell(11).setCellValue("生日");


        // 遍历上面数据库查到的数据
        for(int i =1;i<=1000000;i++){

            //填充数据
            SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            //序号
            dataRow.createCell(0).setCellValue(i);
            //看你实体类在进行填充
            dataRow.createCell(1).setCellValue("NAME"+i);
            dataRow.createCell(2).setCellValue(i%2);
            dataRow.createCell(3).setCellValue(i%3);
            dataRow.createCell(4).setCellValue(i%4);
            dataRow.createCell(5).setCellValue(i%5);
            dataRow.createCell(6).setCellValue(i%6);
            dataRow.createCell(7).setCellValue(i%7);
            dataRow.createCell(8).setCellValue(i%8);
            dataRow.createCell(9).setCellValue(i%9);
            dataRow.createCell(10).setCellValue(i%10);
            dataRow.createCell(11).setCellValue(i%11);
        }

        // 下载导出
        String filename = "导出excel表格名字";
        // 设置头信息
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        //一定要设置成xlsx格式
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
        //创建一个输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //写入数据
        sxssfWorkbook.write(outputStream);

        // 关闭
        outputStream.close();
        sxssfWorkbook.close();
    }

测试了一下100w条数据大概十几秒之后开始下载。

 

 

你可能感兴趣的:(后端,spring,boot,java,后端)