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();
}
运行后: