阿里easyexcel框架实现动态数据列导出问题

qq_35712166 2020-08-07 06:05:39


如上图所示,有两个动态列,这种使用easyexcel框架来导出的话要怎么处理,求大神指点。

PS:目前本人就只会用基本的注解导出固定列数的数据。
...全文
11950 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
CodeDevMaster 2022-07-28
  • 打赏
  • 举报
回复
    public void exportHouseInfoList(HttpServletResponse response) throws Exception {
        // 构建动态表头
        List<List<String>> headList = new ArrayList<>();
        String[] HouseCostHead = new String[]{"类型", "公司", "分公司", "需求部门"};
        List<String> tempHead;
        for (String head : HouseCostHead) {
            tempHead = new ArrayList<>();
            tempHead.add(head);
            headList.add(tempHead);
        }

        // 封装数据
        List<List<Object>> infoList = new ArrayList<>();
        List<Object> hInfo = new ArrayList<>();
        List<Map<String, Object>> hList = hostelDao.getHouseInfoList();
        for (Map<String, Object> map : hList) {
            hInfo.add(map.get("type"));
            hInfo.add(map.get("company"));
            hInfo.add(map.get("office"));
            infoList.add(hInfo);
        }

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet houseSheet = EasyExcel.writerSheet(1, "房屋信息").head(headList).build();
        String houseExcel = BaseUtil.formatDate(new Date()) + "房屋信息表.xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(houseExcel, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        excelWriter.write(infoList, houseSheet);
        excelWriter.finish();
    }
Lning_yuan 2022-07-25
  • 打赏
  • 举报
回复

同学,这个地方怎样解决的

wycfight 2020-11-05
  • 打赏
  • 举报
回复
解决了吗 方案是什么。 数据先读取 组装表头就行了?
Lning_yuan 2022-07-25
  • 举报
回复
@wycfight 这个地方有解决方案了吗 同学
  • 打赏
  • 举报
回复
你注意数据结构要对应上。可以取一条数据测试。找到原因先
staton_ 2020-09-22
  • 打赏
  • 举报
回复 4
head使用List<List<String>> 的数据结构, WriteTable table = new WriteTable(); table.setHead(headList); 相应的,内容也需要为List<List<String>> 的数据结构
未来好久不见 2020-09-21
  • 打赏
  • 举报
回复
我也想知道怎么做到
KeepSayingNo 2020-09-21
  • 打赏
  • 举报
回复
动态列头把它当数据,在导出样式中加上样式,就是对于你这种需要新做一个模板类去处理
tianfang 2020-09-21
  • 打赏
  • 举报
回复
先读表头 再输出 你的输出需求(要求)是什么
qq_35712166 2020-08-22
  • 打赏
  • 举报
回复
没有大兄弟来回复下么。。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧