EasyExcel导出多sheet页

实现效果

EasyExcel导出多sheet页_第1张图片

 

依赖项

        
            com.alibaba
            easyexcel-core
            3.2.1
            compile
        
        
            com.alibaba
            easyexcel
            3.2.1
            compile
        

controller层

    @PostMapping("/export")
    public void export(HttpServletResponse response, String vin) throws IOException {
        service.excelTest(response, vin);
    }

serviceImpl

    @Override
    public void excelTest(HttpServletResponse response, String vin) {
        // 车身信息
        PlanCarInfo planCarInfos = stopMapper.getPlanCarInfoByVin(vin);
        // 过点记录
        List aviPassRecords = stopMapper.getAviPassRecordInfoByVin(vin);
        // 化验参数
        List paramAssConfigs = assayService.findAllParamAssConfigInfo(vin);

        Map map = new HashMap<>();
        map.put("carType",planCarInfos.getBodyType());
        map.put("carColor",planCarInfos.getColorType());
        map.put("carConfig",planCarInfos.getConfigType());
        map.put("tpsCode",planCarInfos.getTpsCode());
        map.put("vin",planCarInfos.getVin());
//

        response.addHeader("Content-Disposition", "filename=" + "模板导出.xlsx");
        //设置类型,扩展名为.xls
        response.setContentType("application/vnd.ms-excel");

        ExcelWriter writer = null;
        try {
            // 车身信息 + 过点记录
            writer = EasyExcel.write(response.getOutputStream()).withTemplate(new ClassPathResource("质量追踪.xlsx").getInputStream()).build();
            WriteSheet writeSheet1 = EasyExcel.writerSheet(0).build();
            writer.fill(map, writeSheet1);
            writer.fill(aviPassRecords, writeSheet1);
            // 化验参数
            WriteSheet writeSheet2 = EasyExcel.writerSheet(1).head(QualityParamAssayDTO.class).build();
            writer.write(paramAssConfigs,writeSheet2);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (writer != null) {
                writer.finish();
            }
        }
    }

你可能感兴趣的:(Java导出,java,idea)