版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ghost_chou/article/details/48863947
前段时间某个项目有个需求,需要从数据库读取数据并导出word文档,于是通过查阅资料,使用freemarker成功实现了该功能。
1.使用office word软件先写好需要导出的文档的大体框架,并保存为xml文件;
2.把该模版对应需要显示数据的地方用${}填充,并把后缀改为.ftl格式放到项目中;
3核心代码:
@SuppressWarnings("resource")
private void createLeaveDoc(Leave leave, String <span style="color:#FF0000;">tempDocPath</span>, <span style="color:#FF0000;">//红色字体表示输出路径</span>
String tempDocName) throws Exception {
<span style="color:#FF0000;">String templatePath =
"D:\\synchronized folder\\version2\\WebRoot\\lea_sys\\template\\";//模版所在路径</span>
Configuration cfg = new Configuration();
cfg.setDefaultEncoding("utf-8");
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
<span style="color:#FF0000;">Template temp = cfg.getTemplate("template.ftl");//加载模版</span>
temp.setEncoding("utf-8");
HashMap contentValue = new HashMap<>();
Integer id = leave.getId();
Integer empId = leave.getEmpId();
String empName = leave.getEmpName();
String empTitle = leave.getEmpTitle();
String office = leave.getOffice();
String department = leave.getDepartment();
String arrivalVerifier = leave.getArrivalVerifier();
Date arrivalDate = leave.getArrivalDate();
Date beginDate = leave.getBeginDate();
Date endDate = leave.getEndDate();
Date submitDate = leave.getSubmitDate();
String place = leave.getPlace();
String reason = leave.getReason();
String type = leave.getType();
Integer requestedLv = leave.getRequestedLv();
String hasFee = leave.getHasFee();
String status = leave.getStatus();
Integer approveCount = leave.getApproveCount();
String approvers = leave.getApprovers();
String arrivalVerifyStatus = leave.getArrivalVerifyStatus();
String rejectReason = leave.getRejectReason();
String icon = leave.getIcon();
String approver1 = leave.getApprover1();
String approver2 = leave.getApprover2();
Date approveDate1 = leave.getApproveDate1();
Date approveDate2 = leave.getApproveDate2();
contentValue.put("id", id);
contentValue.put("empId", empId);
contentValue.put("empName", empName);
contentValue.put("empTitle", empTitle);
contentValue.put("office", office);
contentValue.put("department", department);
contentValue.put("arrivalVerifier", arrivalVerifier);
contentValue.put("arrivalDate", arrivalDate);
contentValue.put("beginDate", beginDate);
contentValue.put("endDate", endDate);
contentValue.put("submitDate", submitDate);
contentValue.put("place", place);
contentValue.put("reason", reason);
contentValue.put("type", type);
contentValue.put("requestedLv", requestedLv);
contentValue.put("hasFee", hasFee);
contentValue.put("status", status);
contentValue.put("approveCount", approveCount);
contentValue.put("approvers", approvers);
contentValue.put("arrivalVerifyStatus", arrivalVerifyStatus);
contentValue.put("rejectReason", rejectReason);
contentValue.put("icon", icon);
contentValue.put("approver1", approver1);
contentValue.put("approver2", approver2);
contentValue.put("approveDate1", approveDate1);
contentValue.put("approveDate2", approveDate2);
File file = new File(tempDocPath, tempDocName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
BufferedWriter bw = new BufferedWriter(osw);
fos.flush();
osw.flush();
bw.flush();
temp.process(contentValue, bw);
fos.close();
osw.close();
bw.close();
logger.debug("输出完成");
}