导出表格(不用dto当表头,用动态表头)

@Override
    public void export(Map<String, Object> params) {
        Object resultData = getDataBysqlSteament(params);
        List<Map<String, Object>> dataList= (List<Map<String, Object>>) resultData;
        Map<String,Object> map = transForExportData(dataList);
        List<List<String>> head = (List<List<String>>) map.get("head");
        List<List<Object>> data = (List<List<Object>>) map.get("data");

        String fileName = "导出数据表.xlsx";
        try {
            //  OutputStream out = new FileOutputStream(fileName);
          //  ExcelWriter excelWriter = EasyExcel.write(out).build();
            //上面的输出流是输出到服务器,单元测试用,下面的输出流是输出到客户端,测试环境生产环境用
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            excelWriter.write(data, EasyExcel.writerSheet().sheetName("导出表").head(head).build());
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static Map<String,Object> transForExportData(List<Map<String,Object>> dataList) {
        Map<String,Object> map = new HashMap<>();
        List<List<String>> head = new ArrayList<>();
        List<List<Object>> data = new ArrayList<>();
        List<String> headerList = new LinkedList<>(dataList.get(0).keySet());
        //构造key
        for (String key : headerList) {
            head.add(new ArrayList<String>() {{
                add(key);
            }});
        }
        //构造data
        List<Object> rowDataList = null;
        for (Map<String,Object> row : dataList) {
            rowDataList = new ArrayList<>();
            for (String key : headerList) {
                rowDataList.add(row.get(key));
            }
            data.add(rowDataList);
        }
        //保存到map返回
        map.put("head",head);
        map.put("data",data);
        return map;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值