首先需要导入freemarker-2.3.19.jar包
然后穿件一个xml文件模板
再次:
Map root= new HashMap();
for (int i = 0; i < listborrownum.size(); i++) {//生成借的凭证
voucherborrow = new VoucherEntrySchema();
voucherborrow.setId(i+1);
voucherborrow.setPayDate(payDate);
voucherborrow.setObjectinfo(listborrownum.get(i).getObjectinfo());
voucherborrow.setPayMoney1(listborrownum.get(i).getPayMoney1());
listborrow.add(voucherborrow);
}
for (int i = 0; i < listcreditnum.size(); i++) {//生成贷的凭证对象
vouchercredit = new VoucherEntrySchema();
vouchercredit.setId(i+2);
vouchercredit.setPayDate(payDate);
vouchercredit.setObjectinfo(listcreditnum.get(i).getObjectinfo());
vouchercredit.setPayMoney2(listcreditnum.get(i).getPayMoney2());
listcredit.add(vouchercredit);
}
root.put("currentMills", "ZJ"+System.currentTimeMillis());//当前时间的毫秒数
root.put("currentDate", currentDate);//当前的日期
root.put("number", number);//生成 entry 的总记录数
root.put("entryborrow", listborrow);
root.put("entrycredit", listcredit);
Configuration config = new Configuration();
URL url=Thread.currentThread().getContextClassLoader().getResource("CreateVoucher.ft");
temp=url.getFile();
temp=temp.substring(1,temp.length());
temp=temp.substring(0,temp.lastIndexOf("/"));
config.setDirectoryForTemplateLoading(new File(temp));
config.setObjectWrapper(new DefaultObjectWrapper());
Template t = config.getTemplate("CreateVoucher.ft");
String currentTime = current.replaceAll("-","");
InputStream in = null;
FileOutputStream fos = null;
File file = null;
String voucher1 = LoadLocationProperties.getPropertiesValue("/location.properties", "voucher1");
try {
System.out.println(voucher1);
file=new File(voucher1+"999999999_"+currentTime+"_LNAE_"+System.currentTimeMillis()+"_"+(Math.random()+"").substring(2)+".xml");
file.getParentFile().mkdirs();
if(!file.exists()){
file.createNewFile();
}
fos = new FileOutputStream(file);
PrintWriter writer = new PrintWriter(fos);
t.process(root, writer);
listborrow.remove(voucherborrow);
listcredit.remove(vouchercredit);
} catch (Exception e) {
e.printStackTrace();
}finally{
fos.flush();
fos.close();