基於easypoi的word和excel模板操作

來源:互聯網
上載者:User

前言:在項目中需要用到合約範本等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("18711111111@139.com");        list.add(p);        p = new Person();        p.setName("小紅");        p.setTel("18711111112");        p.setEmail("18711111112@139.com");        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:完畢。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.