easyExcel复杂表头导出


    @Override
    public void accountStatCollectExport(AccountStatPageListVO accountStatPageListVO, HttpServletResponse response) throws Exception {
        //应收项目
        R<List<BilledProject>> billedProjectListByTenantId = smartPropertyClient.getBilledProjectListByTenantId(accountStatPageListVO.getTenantId());
        List<BilledProject> accountReceivableData = billedProjectListByTenantId.getData();
        //实收项目
        R<List<ProjectRealIncome>> projectRealIncomeByTenantId = smartPropertyClient.getProjectRealIncomeByTenantId(accountStatPageListVO.getTenantId());
        List<ProjectRealIncome> actualData = projectRealIncomeByTenantId.getData();

        String fileName = "汇总合同账单统计"; // 设置导出的文件名
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

        //插入的数据
        List<List<Object>> contentList = Lists.newArrayList();
        List<AccountStatPageListRespVO> dataList = accountStatCollectList(accountStatPageListVO);
        for (AccountStatPageListRespVO vo : dataList) {
            ArrayList<Object> data = new ArrayList<>();
            data.add(vo.getAccountSubject());
            data.add(vo.getLeasee());
            data.add(vo.getElectricOpenBalance());
            data.add(vo.getRentOpenBalance());
            data.add(vo.getOpenBalance());
            List<ReceivableStat> receivableStat = vo.getReceivableStat();
            for (ReceivableStat stat : receivableStat) {
                data.add(stat.getFeeBillTotalAmt());
            }
            data.add(vo.getReceivableAmt());
            List<ReceiveStat> receiveStat = vo.getReceiveStat();
            for (ReceiveStat stat : receiveStat) {
                data.add(stat.getReceivedTotalAmt());
            }
            data.add(vo.getReceiveAmt());
            data.add(vo.getElectricCloseBalance());
            data.add(vo.getRentLoseBalance());
            data.add(vo.getCloseBalance());
            contentList.add(data);
        }

        // 设置导出的表头结构
        List<List<String>> head = createCollectHead(accountReceivableData, actualData);
        MyEasyExcelUtil.download(response, fileName, head, contentList);
    }


private List<List<String>> createCollectHead(List<BilledProject> billedProjects,
                                                 List<ProjectRealIncome> projectRealIncomes) {
        List<List<String>> head = new ArrayList<>();
        String should = "本期借方(应收)", borrow = "本期贷方(收回)";
        //第一列,1/2行
        head.add(Lists.newArrayList("合同号", "合同号"));
        //第二列,1/2行
        head.add(Lists.newArrayList("承租方", "承租方"));
        head.add(Lists.newArrayList("期初余额", "期初余额", "电费"));
        head.add(Lists.newArrayList("期初余额", "期初余额", "房租"));
        head.add(Lists.newArrayList("期初余额", "期初余额", "期初余额"));

        List<String> list = new ArrayList<>();
        for (BilledProject project : billedProjects) {
            list.add(project.getName());
        }
        list.forEach(title -> {
            head.add(Lists.newArrayList(should, should, title));
        });
        head.add(Lists.newArrayList(should, should, "小计"));

        List<String> list2 = new ArrayList<>();
        for (ProjectRealIncome project : projectRealIncomes) {
            list2.add(project.getName());
        }
        list2.forEach(title -> {
            head.add(Lists.newArrayList(borrow, borrow, title));
        });
        head.add(Lists.newArrayList(borrow, borrow, "小计"));

        head.add(Lists.newArrayList("本期余额", "本期余额", "智能电费"));
        head.add(Lists.newArrayList("本期余额", "本期余额", "房租等"));
        head.add(Lists.newArrayList("本期余额", "本期余额", "期末余额"));
        return head;
    }


    /**
     * 导出execl
     *
     * @param response 输出流
     * @param fileName 文件名
     * @param headList 表头
     * @param data     数据
     * @throws IOException
     */
    public static void download(HttpServletResponse response, String fileName, List<List<String>> headList, List data) throws IOException {
        String excelName = StringUtil.isNotBlank(fileName) ? fileName : "" + DateUtil.format(new java.util.Date(), "yyyy-MM");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        excelName = URLEncoder.encode(excelName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + excelName + ".xls");
        EasyExcel.write(response.getOutputStream()).head(headList).excelType(ExcelTypeEnum.XLS).sheet("sheet1").doWrite(data);
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南_烟雨.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值