excel导出时不建模板类,动态导出

1.Controller

 /**
     * 自定义导出
     *
     * @param
     * @return
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam("userIdListStr") String userIdListStr, @RequestParam("fieldNameListStr") String fieldNameListStr) throws Exception {
        List<String> userIdList = Arrays.asList(userIdListStr.split(","));
        List<String> fieldNameList = Arrays.asList(fieldNameListStr.split(","));
        dingRosterService.myExport(request, response, userIdList, fieldNameList);
    }

2.service

    /**
     * 自定义导出
     *
     * @param userIdList
     * @param request
     * @param response
     * @param fieldNameList
     * @return
     */
    void myExport(HttpServletRequest request, HttpServletResponse response,List<String> userIdList, List<String> fieldNameList) throws UnsupportedEncodingException;

3.serviceImpl

@Override
    public void myExport(HttpServletRequest request, HttpServletResponse response, List<String> userIdList, List<String> fieldNameList) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //在设置response.setHeader时,如果含有中文字符,一定要转换成ISO8859-1格式,否则设置的中文会出现错误。
        String excelName = "员工花名册信息自定义导出    " + dateFormat.format(date)+".xls";
        response.setHeader("content-disposition", "attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1"));
        try {
            // 这里需要设置不关闭流
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            //设置背景颜色
            headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
            //内容策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            //设置 水平居中
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
            EasyExcel.write(response.getOutputStream()).autoCloseStream(Boolean.FALSE)
                    .registerWriteHandler(new CustomizeColumnWidth()).registerWriteHandler(horizontalCellStyleStrategy)
                    .head(myriadPeopleHead(fieldNameList)).sheet("模板")
                    //获取数据填充,按顺序匹配
                    .doWrite(getMyriadPeopleReportData(userIdList, fieldNameList));
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            try {
                response.getWriter().println(JSON.toJSONString(map));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    private List getMyriadPeopleReportData(List<String> userIdList, List<String> fieldNameList) {
        List<List<String>> listList = new ArrayList<>();
        for (String userId : userIdList) {
            List<String> list = new ArrayList<>();
            for (String fieldName : fieldNameList) {
                String fieldList = baseDao.selectExport(fieldName, userId);
                if (fieldList != null) {
                    List<FieldListDTO> fieldListDTOS = JSON.parseArray(fieldList, FieldListDTO.class);
                    if (fieldListDTOS.size() > 1) {
                        String label = "";
                        int i = 0;
                        for (FieldListDTO fieldListDTO : fieldListDTOS) {
                            i++;
                            label = label + "第" + i + fieldName + "为" + fieldListDTO.getLabel() + ";";
                            fieldList = label;
                        }
                    } else {
                        for (FieldListDTO fieldListDTO : fieldListDTOS) {
                            fieldList = fieldListDTO.getLabel();
                        }
                    }
                    list.add(fieldList);
                }
            }
            listList.add(list);
        }
        return listList;
    }

    /**
     * 创建自定义表头
     *
     * @param fieldNameList
     * @return
     */
    private List<List<String>> myriadPeopleHead(List<String> fieldNameList) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<List<String>> list = new ArrayList<List<String>>(10);
        for (String fieldName : fieldNameList) {
            List<String> head = new ArrayList<String>(2);
            head.add("自定义导出花名册数据信息    " + dateFormat.format(date));
            head.add(fieldName);
            list.add(head);
        }
        return list;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值