java按模板生成word文档

java按模板生成word文档

首先看模板样式
在这里插入图片描述

导入依赖

<!--word模板生成相关-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.24</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.5.0</version>
        </dependency>

代码

public static void main(String[] args) {
        Map<String, Object> map  = new HashMap<>();
        map.put("examneename","乌尔");//考生姓名
        map.put("gender","男");//性别
        map.put("age","34");//年龄
        map.put("examnum","1101010101111");//准考证号
        map.put("idcard","110302198907210056");//身份证
        map.put("examunit","大银行");//报名单位
        map.put("exampost","大堂经理");//报名岗位
        //图片生成
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setHeight(190);//图片高度
        imageEntity.setWidth(140);//图片宽度
        imageEntity.setUrl("D:\\intelliJ_workspace\\人事管理报名系统\\文件\\大美丽.png");//图片路径
        imageEntity.setType(ImageEntity.URL);
        map.put("testcode",imageEntity);//图片对象
        try {
        //模板路径
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "D:\\intelliJ_workspace\\人事管理报名系统\\文件\\准考证.docx",map);
            //生成的文档路径
            FileOutputStream fos = new FileOutputStream(
                    "D:\\intelliJ_workspace\\人事管理报名系统\\文件\\image3.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行main方法
最后看结果:
在这里插入图片描述

循环表格生成
引入依赖

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>ooxml-schemas</artifactId>
     <version>1.4</version>
</dependency>

模板表格准备
在这里插入图片描述

 public static void main(String[] args) {
//
        Map<String, Object> map  = new HashMap<>();
        map.put("examneename","乌尔");//考生姓名
        map.put("gender","男");//性别
        map.put("age","34");//年龄
        map.put("examnum","1101010101111");//准考证号
        map.put("idcard","110302198907210056");//身份证
        map.put("examunit","大银行");//报名单位
        map.put("exampost","大堂经理");//报名岗位
        //图片生成
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setHeight(190);//图片高度
        imageEntity.setWidth(140);//图片宽度
        imageEntity.setUrl("D:\\intelliJ_workspace\\人事管理报名系统\\文件\\大美丽.png");//图片路径
        imageEntity.setType(ImageEntity.URL);
        map.put("testcode",imageEntity);//图片对象

        //模拟表格数据
        ArrayList<HashMap<String,String>> list1 = new ArrayList<>(2);
        HashMap<String,String> temp = new HashMap<>(6);
        temp.put("sn","2022.11.14 09:00 - 2022.11.14 10:00");
        temp.put("subject","公共安全");
        temp.put("examaddress","信阳市浉河区某某路**号");
        temp.put("examsite","幸福中学");
        temp.put("examroom","第一考场");
        temp.put("examseatnum","1");
        list1.add(temp);
        temp = new HashMap<>(6);
        temp.put("sn","2022.11.14 10:30 - 2022.11.14 11:30");
        temp.put("subject","岗位技能");
        temp.put("examaddress","信阳市浉河区某某路**号");
        temp.put("examsite","幸福中学");
        temp.put("examroom","第一考场");
        temp.put("examseatnum","1");
        list1.add(temp);
        temp = new HashMap<>(6);
        temp.put("sn","2022.11.14 14:30 - 2022.11.14 15:30");
        temp.put("subject","职业能力");
        temp.put("examaddress","信阳市浉河区某某路**号");
        temp.put("examsite","幸福中学");
        temp.put("examroom","第一考场");
        temp.put("examseatnum","1");
        list1.add(temp);
        map.put("examinfolist",list1);

        try {
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "D:\\intelliJ_workspace\\人事管理报名系统\\文件\\准考证01.docx",map);
            FileOutputStream fos = new FileOutputStream(
                    "D:\\intelliJ_workspace\\人事管理报名系统\\文件\\image3.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

运行后:
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值