java根据word模板生成word文件

本文使用的feemarker实现的功能
1、首先新建一个word模板,
2、另存为xml格式,
3、将xml文件的扩展名直接改成ftl,
4、导入freemaker.jar
在这里插入图片描述
2、使用软件打开ftl文件,找到w:binData快速定位图片位置,写上${imgStr}(名字随意,和java代码中的对应上即可),然后保存。
3、因涉及到list的问题,找到需要设计list的行,找到w:tr,<#list></#list>将w:tr包裹。
在这里插入图片描述
在这里插入图片描述
3、新建java类

@Component
public class ExamTemplate {
	
	public String saveExamTable(Welder welder,ExamRegis examRegis,List<WorkExperience> wExperiences,List<WeldQua> weldQuas,
			List<Train> trains,List<ExamProject> examProjects,String templateUrl,Inframework inframework,HttpServletRequest req)throws Exception {
		Map<String, Object> dataMap=new HashMap<String, Object>();
		
			dataMap.put("depName", inframework.getInframeworkName());
			dataMap.put("code", inframework.getCode());
			dataMap.put("address", inframework.getAddress());
			dataMap.put("linker", inframework.getLinker());
			String regisNum=examRegis.getRegisNum();
			dataMap.put("regisNum", regisNum);
			//dataMap.put("startTime", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse("2018-09-19")));
			dataMap.put("idNumber", welder.getIdNumber());
			dataMap.put("name",welder.getName());
			int sexInt=welder.getSex();
			if(sexInt==1) {
				dataMap.put("sex", "男");
			}else {
				dataMap.put("sex", "女");
			}
			//dataMap.put("fukuan", "test");
			dataMap.put("imgStr",getImageStr(welder,templateUrl));
			 
			dataMap.put("theoryNum",examRegis.getTheoryNum());
			//dataMap.put("workExperience", "叁仟肆佰万");
			//List<WeldQua> weldQuas=new ArrayList<WeldQua>();
			
			dataMap.put("wExperiences", wExperiences);
			
			dataMap.put("weldQuas", weldQuas);
			
			dataMap.put("trains", trains);
			
			dataMap.put("examProjects", examProjects);
			Configuration configuration=new Configuration();
			configuration.setDefaultEncoding("utf-8");
			configuration.setDirectoryForTemplateLoading(new File(templateUrl));
			Calendar calendar=Calendar.getInstance();
			int monthInt=calendar.get(Calendar.MONTH)+1;
			int year=calendar.get(Calendar.YEAR);
			String newName=UUID.randomUUID().toString();
			File outFile=new File(templateUrl+year+"/"+monthInt+"/"+newName+".doc");
			Template template=configuration.getTemplate("报名表.ftl","utf-8");
			Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"),1024);
			template.process(dataMap,out);
			out.flush();
			out.close();
			String url=req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+"/WeldCloud/upload/"+year+"/"+monthInt+"/"+newName+".doc";
			return url;
	}
	public String getImageStr(Welder welder,String templateUrl) throws Exception{
		String[] path=welder.getPhotoUrl().split("/");
		String imgFile="";
		if(path.length==8) {
			imgFile=templateUrl+path[5]+"/"+path[6]+"/"+path[7];
		}
		InputStream in=null;
		byte[] data=null;
		in=new FileInputStream(imgFile);
		data=new byte[in.available()];
		in.read(data);
		in.close();
		BASE64Encoder encoder=new BASE64Encoder();
		return encoder.encode(data);
	}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值