基于easypoi的word和excel模板操作

前言:在项目中需要用到合同模板等word模板操作,通过查阅网络资料博客,发现easypoi是一个比较好的第三方库,并在项目中进行了使用,特辑之。

1、首先,进入easypoi的官方指导博客进行文档阅读,参考链接:http://easypoi.mydoc.io/

2、gradle导入maven版本仓库:

compile 'org.jeecg:easypoi-base:2.1.3'
compile 'org.jeecg:easypoi-web:2.1.3'
compile 'org.jeecg:easypoi-annotation:2.1.3'

3、编码实现:

jsp: 导出word 

4、controller代码实现:(demo)

/**
     * 导出Word测试
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "MapExportWordTest")
    public String SimpleWordExport(HttpServletRequest request, HttpServletResponse response
            ,  ModelMap modelMap) {

        String filePath = request.getSession().getServletContext().getRealPath("/export/" + "SimpleExcel.docx");
        Map map = new HashMap();
        map.put("department", "Easypoi");
        map.put("person", "JueYue");
        map.put("time", format.format(new Date()));
        List list = new ArrayList();
        Person p = new Person();
        p.setName("小明");
        p.setTel("18711111111");
        p.setEmail("[email protected]");
        list.add(p);
        p = new Person();
        p.setName("小红");
        p.setTel("18711111112");
        p.setEmail("[email protected]");
        list.add(p);
        map.put("pList", new ExcelListEntity(list, Person.class));        modelMap.put(TemplateWordConstants.FILE_NAME,"Word测试");
        modelMap.put(TemplateWordConstants.MAP_DATA,map);
        modelMap.put(TemplateWordConstants.URL,filePath);
        return TemplateWordConstants.JEECG_TEMPLATE_WORD_VIEW;
    }

5、项目应用:(从数据库中查取关联数据插入到word中{demandName}标识数据)

/**
     * 下载合同
     *
     * @param contractId
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "downloadContract")
    public String downloadContract(@RequestParam("contractId") int contractId, HttpServletRequest request, HttpServletResponse response
            , HttpSession session, ModelMap modelMap) {

        Contract contract = contractService.getContractDetailById(contractId);
        Templet templet = contract.getTemplet();
        String dir = uploadTempletDir + templet.getTempletType().name() + "/" + templet.getTempletName() + "/" + contract.getVersionNo();

        String realFilePath = request.getSession().getServletContext().getRealPath(dir);
        ArrayList fileTempletLogVos = ScanDirUtils.getFileTempletLogVos(realFilePath, templet.getTempletType(), templet.getTempletName());

        if (CollectionUtils.isEmpty(fileTempletLogVos)) {
            String url ="demand/viewFinishedDemand.do?demandId"+contract.getDemand().getDemandId();
            return "redirect:../common/failure.do?msg=error_downLoad_failure&&url="+url;
        }

        Demand demand = contract.getDemand();
        //如果已经中标,查询中标记录
//        if (demand.getDemandState() == DemandState.BIDDED||demand.getDemandState() == DemandState.EDIT_CONTRACT) {
        //查询 中标记录
        Bid bid = bidService.getWinBiddingByDemandId(demand.getDemandId());
        modelMap.addAttribute("bid", bid);
//        }


        Company company = demand.getCompany();
        String dateStr = format.format(contract.getCreateTime());


        String downFilePath = realFilePath+"/"+fileTempletLogVos.get(0).getFileName();
        Map map = new HashMap();
        map.put("companyName", company.getCompanyName());
        map.put("designerName", bid.getCustomer().getCustomerName());
        map.put("createTime", dateStr);

        modelMap.put(TemplateWordConstants.FILE_NAME, contract.getContractName());
        modelMap.put(TemplateWordConstants.MAP_DATA, map);
        modelMap.put(TemplateWordConstants.URL, downFilePath);
        return TemplateWordConstants.JEECG_TEMPLATE_WORD_VIEW;
    }
6:完毕。




你可能感兴趣的:(java)