jeecg vue导出excel

helei 2022-3-24 482 3/24
 @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, @RequestParam(name="keys" ,defaultValue = "") List<String> keys, @RequestParam(name="id",defaultValue = "") String id) throws IOException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("salary_fa_id",id);
        List<YzhrSalaryDetail> list = iYzhrSalaryDetailService.list(queryWrapper);
        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
        List<ExcelExportEntity> entityList = new ArrayList<>();
        //添加普通表头
        ExcelExportEntity e1 = new ExcelExportEntity("姓名","姓名");
        ExcelExportEntity e2 = new ExcelExportEntity("部门","部门");
        entityList.add(e1);
        entityList.add(e2);
        //添加表头合成,设置主表头
        ExcelExportEntity e4 = new ExcelExportEntity("应发加项","应发加项",true);
        List<String> sub = new ArrayList<>();
        ExcelExportEntity ekou = new ExcelExportEntity("应扣款项","应扣款项",true);
        List<String> subkou = new ArrayList<>();
        Gson gson = new Gson();
        Map<String ,Object> map1 =  gson.fromJson(list.get(0).getDetail(),new TypeToken<Map<String, Object>>() {}.getType());
        for (String key :map1.keySet()) {
            log.info("详情的key"+key.toString());
        }
        Map<String ,Object> mapkou =  gson.fromJson(map1.get("faDetail").toString(),new TypeToken<Map<String, Object>>() {}.getType());
        for (String key :mapkou.keySet()) {
            log.info("详情的key"+key.toString());
            //添加表头合成,设置子表头
            ExcelExportEntity e5 = new ExcelExportEntity(key,key,true);
            entityList.add(e5);
            sub.add(key);
        }
        //添加表头集合
        e4.setSubColumnList(sub);
        entityList.add(e4);
        //应发合计表头
        ExcelExportEntity allfa = new ExcelExportEntity("应发合计","应发合计");
        entityList.add(allfa);
        Map<String ,Object> mapfa =  gson.fromJson(map1.get("kouDetail").toString(),new TypeToken<Map<String, Object>>() {}.getType());
        for (String key :mapfa.keySet()) {
            log.info("详情的key"+key.toString());
            //应扣子表头
            ExcelExportEntity e5 = new ExcelExportEntity(key,key,true);
            entityList.add(e5);
            subkou.add(key);
        }
        ekou.setSubColumnList(subkou);
        entityList.add(ekou);
        //应扣合计表头
        ExcelExportEntity allkou = new ExcelExportEntity("应扣合计","应扣合计");
        entityList.add(allkou);
        //实发薪资表头
        ExcelExportEntity all = new ExcelExportEntity("实发薪资","实发薪资");
        entityList.add(all);
        for (YzhrSalaryDetail salaryDetail :list) {
            Map<String ,Object> mapde =  gson.fromJson(salaryDetail.getDetail(),new TypeToken<Map<String, Object>>() {}.getType());
            Map map = new HashMap();
            SysUser user = iUserService.getById(salaryDetail.getStaffBaseId());
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("org_code",user.getOrgCode());
            SysDepart sysDepart = iYzDepartService.getOne(queryWrapper1);
            map.put("姓名",user.getRealname());
            map.put("部门",sysDepart.getDepartName());
            map.put("应发合计",mapde.get("fa"));
            map.put("应扣合计",mapde.get("kou"));
            map.put("实发薪资",mapde.get("all"));
            Map<String ,Object> mapkoude =  gson.fromJson(mapde.get("faDetail").toString(),new TypeToken<Map<String, Object>>() {}.getType());
            for (String key :mapkoude.keySet()) {
                map.put(key,mapkoude.get(key));
            }
            Map<String ,Object> mapfade =  gson.fromJson(mapde.get("kouDetail").toString(),new TypeToken<Map<String, Object>>() {}.getType());
            for (String key :mapfade.keySet()) {
                map.put(key,mapfade.get(key));
            }
            dataList.add(map);
        }
        Workbook wb = ExcelExportUtil.exportExcel(new ExportParams(null, "薪资明细表"),entityList,dataList);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;fileName=薪资明细.xls");
        response.setContentType("application/octet-stream;charset=utf-8");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
        //return Result.OK("生成成功");
    }
//导出excel
handleExportXls() {
this.$message.loading("导出中请不要关闭页面!")
downFile("/staffAll/exportExcel?id="+this.id, "").then(res => {
console.log(res)
let url = window.URL.createObjectURL(new Blob([res]));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', '薪资明细表.xls');
document.body.appendChild(link);
link.click()
this.$message.destroy();
})
}
/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url,parameter){
  return axios({
    url: url,
    params: parameter,
    method:'get' ,
    responseType: 'blob'
  })
}

- THE END -

helei

3月24日17:42

最后修改:2022年3月24日
0

非特殊说明,本博所有文章均为博主原创。