Easypoi excel template export

Template export is similar to simple export, except that the controller layer is slightly different. In addition, an export template must be prepared.

For other codes, click Easypoi's excel simple export
to prepare an export template.
Insert image description here
Insert image description here

controller layer code

    /**
     * 按模板导出数据
     * @param response
     */
    @PostMapping(value = "/exportByTem",produces = "application/octet-stream")
    public void exportJg(HttpServletResponse response,HttpServletRequest request){
    
    
        /**
         *如果是在SSM项目中用以下方式,模板直接放在webapp下
         * String path = request.getSession().getServletContext().getRealPath("/")+"template/技工报名表.xls";
         * TemplateExportParams params = new TemplateExportParams(path);
         */
        //取到要导出的模板
        TemplateExportParams params = new TemplateExportParams("\\static\\template\\报名表.xls");
        if (null!=params){
    
    
            List<Applys> list = ApplyService.queryAll();
            Map<String, Object> map = new HashMap<String, Object>();
            List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
            for (int i = 0; i < list.size(); i++) {
    
    
                Map<String, String> lm = new HashMap<String, String>();
                //put的键要跟excel模板中的名称一致
                lm.put("id", i + 1 + "");
                lm.put("examNo", list.get(i).getExamNo());
                lm.put("realName", list.get(i).getRealName());
                lm.put("sex", list.get(i).getSex());
                lm.put("idNo", list.get(i).getIdNo());
                lm.put("homeTown",list.get(i).getHomeTown());
                lm.put("highestDegree",list.get(i).getHighestDegree());
                lm.put("workYears",list.get(i).getWorkYears());
                lm.put("workTypeName",list.get(i).getWorkTypeName());
                lm.put("workTypeCode",list.get(i).getWorkTypeCode().toString());
                lm.put("skillGrade",list.get(i).getSkillGrade());
                lm.put("skillGradeCode",list.get(i).getSkillGradeCode());
                lm.put("workCompany",list.get(i).getWorkCompany());
                listMap.add(lm);
            }
            map.put("maplist", listMap);
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            ServletOutputStream out = null;
            try {
    
    
                //流的形式传输数据
                response.setHeader("content-type","application/octet-stream");
                //防止中文乱码
                response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("报名表.xls","UTF-8"));
                out=response.getOutputStream();
                workbook.write(out);
            }catch (IOException e){
    
    
                e.printStackTrace();
            }finally {
    
    
                if (null!=out){
    
    
                    try {
    
    
                        out.close();
                    } catch (IOException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }
    }

Guess you like

Origin blog.csdn.net/qq_42042158/article/details/121960909