Word模板文件,替换内容 文本、图片、表格、列表、区块、嵌套等

Word模板文件,替换内容 文本、图片、表格、列表、区块、嵌套等

实例:员工信息表Word文件,程序动态替换内容,生成pdf文件

 话不多说,先看效果图!

优势:

1、模板只是布局调整,程序无需修改! 

2、以往做法,写成HTML 页面方便替换内容,再生成word,这样做存在错位兼容问题! 

 

直接贴代码:

第一步:pom.xml

    <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.7.3</version>
        </dependency>

官方文档地址:http://deepoove.com/poi-tl/#_%E6%96%87%E6%9C%AC

第二步:实例代码

package cn.com.poi;


import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.deepoove.poi.util.BytePictureUtils;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class word {


    @Test
    public void test() throws IOException {

        String fie = "D:\\员工信息登记表.docx";
        String output = "D:\\output.docx";

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("name", "刘**");
        userInfo.put("sex", "男");
        userInfo.put("age", "30岁");
        userInfo.put("birth", "1990年");
        userInfo.put("en_name", "Saimen");
        userInfo.put("country", "中国");
        userInfo.put("province", "广东");
        userInfo.put("city", "深圳");
        userInfo.put("nation", "汉族");
        userInfo.put("telephone", "137******3");
        userInfo.put("email", "326427540@qq.com");
        userInfo.put("hobby", "TV 电视 电影 代码");
        userInfo.put("body", "健康");
        userInfo.put("political", "☐群众 ☐团员 ☐预备党员 ☐党员 ☑其他");

        userInfo.put("xz_province", "广东");
        userInfo.put("xz_city", "深圳");
        userInfo.put("xz_area", "宝安");
        userInfo.put("xz_adder", "西乡街道办");

        userInfo.put("yj_province", "江西");
        userInfo.put("yj_city", "九江");
        userInfo.put("yj_area", "鄱阳湖");
        userInfo.put("yj_adder", "鄱阳湖水边");

        userInfo.put("hk_province", "广东");
        userInfo.put("hk_city", "深圳");
        userInfo.put("hk_nature", "☐农村户口 ☑城镇户口");

        userInfo.put("id_card_type", "身份证");
        userInfo.put("id_card_number", "363************X");
        userInfo.put("bank_name", "平安银行");
        userInfo.put("bank_number", "378************1");

        userInfo.put("marriage", "☐未婚(☐单身  ☐非单身)☑已婚 ☐离异 ☐丧偶");
        userInfo.put("childbirth", "☐未生育 ☐已育1孩 ☑已育2孩  ☐已育2孩以上");
        userInfo.put("english_ability", "英语超级好");
        userInfo.put("other_ability", "计算机语言");
        userInfo.put("computer_grade", "四级");
        userInfo.put("titles_type", "首席技术官");
        userInfo.put("titles_level", "AAAAA级");
        userInfo.put("social_security_no", "66666");
        userInfo.put("accumulation_fund_no", "999999");
        // 网络图片
        userInfo.put("image", new PictureRenderData(92, 120, ".jpg",
                BytePictureUtils.getUrlBufferedImage("https://avatar.csdnimg.cn/4/9/F/1_qq_15371293_1593430328.jpg")
        ));
        //统招
        HashMap<String, Object> t_school_map = new HashMap<String, Object>() {{
            put("from_start_date", "2020/07/03");
            put("from_end_date", "2020/07/05");
            put("school", "**大学");
            put("specialty", "***专业");
            put("education", "本科");
            put("graduation_type", "统招");
            put("if_max", "☑是 ☐否");
        }};
        List<Object> t_school = new ArrayList<>();
        t_school.add(t_school_map);
        userInfo.put("t_school", t_school);
        //非统招
        HashMap<String, Object> ft_school_map = new HashMap<String, Object>() {{
            put("from_start_date", "1993/07/03");
            put("from_end_date", "1995/07/05");
            put("school", "爱心幼儿园");
            put("specialty", "大班");
            put("education", "幼儿园");
            put("graduation_type", "非统招");
            put("if_max", "☑是 ☐否");
        }};
        List<Object> ft_school = new ArrayList<>();
        ft_school.add(ft_school_map);
        userInfo.put("ft_school", ft_school);
        //工作信息
        HashMap<String, Object> work_map = new HashMap<String, Object>() {{
            put("from_start_date", "1993/07/03");
            put("from_end_date", "1995/07/05");
            put("where_work", "A公司");
            put("work_department", "研发中心");
            put("job_name", "首席技术官");
            put("superiors", "老板");
            put("telephone", "13760330153");
        }};
        List<Object> work = new ArrayList<>();
        work.add(work_map);
        userInfo.put("work", work);
        //培训经验
        HashMap<String, Object> train_map = new HashMap<String, Object>() {{
            put("from_start_date", "2020/07/3");
            put("from_end_date", "2020/07/05");
            put("training_program", "无");
            put("training_agency_name", "无");
            put("certificate", "无");
        }};
        List<Object> train = new ArrayList<>();
        train.add(train_map);
        userInfo.put("train", train);
        //资格证书获得情况
        HashMap<String, Object> qualification_map = new HashMap<String, Object>() {{
            put("certificate_name", "英语6级");
            put("giver_unit", "英语机构");
            put("certificate_date", "2001-02-06");
            put("effective_date", "永久");

        }};
        List<Object> qualification = new ArrayList<>();
        qualification.add(qualification_map);
        userInfo.put("qualification", qualification);
        //family
        HashMap<String, Object> family_map = new HashMap<String, Object>() {{
            put("family_relationship", "父亲");
            put("family_name", "刘**");
            put("family_workplace", "大自然");
            put("occupation", "五谷杂粮");
            put("telephone", "13******");
            put("is_sos", "\uF052☑是 ☐否");
        }};
        List<Object> family = new ArrayList<>();
        family.add(family_map);
        userInfo.put("family", family);


        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder()
                .bind("t_school", policy)
                .bind("ft_school", policy)
                .bind("work", policy)
                .bind("train", policy)
                .bind("qualification", policy)
                .bind("family", policy)
                .build();


        XWPFTemplate template = XWPFTemplate.compile(fie, config).render(userInfo);
        FileOutputStream out = new FileOutputStream(output);
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }


}

第三步:下载演示文件    http://note.youdao.com/s/TnLKuWGq

为 deepoove 点赞 

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值