基於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:<a href="MapExportWordTest.do" class="btn btn-primary btn-quirk"><i class="fa fa-check"></i> 導出word </a>

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<String, Object> map = new HashMap<String, Object>();
        map.put("department", "Easypoi");
        map.put("person", "JueYue");
        map.put("time", format.format(new Date()));
        List<Person> list = new ArrayList<Person>();
        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<TempletLogVo> 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<String, Object> map = new HashMap<String, Object>();
        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:完畢。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章