使用easypoi实现数据库2个表导出成excel(2个sheet)

1.导入依赖

  <!-- 导出文件工具 EasyPoi实现Excel读写管理测试用例  -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

2.业务代码

 @Override
    public void dataxport(HttpServletResponse response) {



        Workbook workBook = null;
        try {
            List<PeopleDatass> peopleDatassList = peopleDatassDao.queryAllByLimit(1, 10);
            System.err.println(JSONArray.toJSONString(peopleDatassList));
            // 创建参数对象(用来设定excel得sheet得内容等信息)
            ExportParams peopleExportParams = new ExportParams();
            // 设置sheet得名称
            peopleExportParams.setSheetName("人口数据大屏");
            // 创建sheet1使用得map
            Map<String, Object> peopleExportMap = new HashMap<>();
            // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
            peopleExportMap.put("title", peopleExportParams);
            // 模版导出对应得实体类型
            peopleExportMap.put("entity", PeopleDatass.class);
            // sheet中要填充得数据
            peopleExportMap.put("data", peopleDatassList);
            //sheet2------------------------------------------------------------
            List<FormulaalgorithmData> selectdatass = formulaalgorithmDataDao.queryAllByLimit(1,10);
            ExportParams empExportParams = new ExportParams();
            empExportParams.setSheetName("指标元数据");
            // 创建sheet2使用得map
            Map<String, Object> selectdatassExportMap = new HashMap<>();
            selectdatassExportMap.put("title", empExportParams);
            selectdatassExportMap.put("entity", FormulaalgorithmData.class);
            selectdatassExportMap.put("data", selectdatass);
            // 将sheet1、sheet2使用得map进行包装
            List<Map<String, Object>> sheetsList = new ArrayList<>();
            sheetsList.add(peopleExportMap);
            sheetsList.add(selectdatassExportMap);
            // 执行方法
            workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            String fileName = new String("数据导出表.xls".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=gb2312");
            workBook.write(response.getOutputStream());
            outputStream.flush();
            byte[] byteArray = outputStream.toByteArray();
             new ByteArrayInputStream(byteArray,0,byteArray.length);
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(workBook != null) {
                try {
                    workBook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值