java根据提供word模板导出word⽂档涉及主要jar包为 freemarker-2.3.10.jar,servlet-api-2.4.jar。
(1)⾸先修改 word模板如下形式,把需要查询写⼊word的值⽤${}形式封装
(2)把 word改为ftl格式
⾸先word另存为选择xml
然后重命名直接将XML⽂件修改后缀名为ftl⽂件
(3)关键代码
public void agentWordDownload(String agentCode, HttpServletRequest request,
HttpServletResponse response) {
//⾸先把word⽣成保存在某⼀地址
boolean Wordsave = this.Wordsave(agentCode, request, response);
//如果⽣成成功则可以下载
if (Wordsave) {
String url = Resource("")
.toString();
//  file:/Svn/20190219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring/            //获取项⽬路径
url = "/" + url.substring(6, url.indexOf("WEB-INF")) +
"downloadFiles" + "/" + "agentInfo" + "/";
//⽂件名字为⽤户代码+.doc
String fileName = agentCode + ".doc";
try {
request.setCharacterEncoding("utf-8");
fileName = new String(fileName);
//获取⽂件路径
String filePath = url + fileName;
filePath = (filePath == null) ? "" : filePath;
response.setContentType("application/x-download");
fileName = de(fileName, "UTF-8");
response.addHeader("Content-Disposition",
"attachment;filename=" + fileName);
FileInputStream fis = null;
OutputStream os = null;
try {
//把word信息写⼊response
os = OutputStream();
fis = new FileInputStream(filePath);
fis = new FileInputStream(filePath);
byte[] b = new byte[1024 * 10];
int i = 0;
while ((i = ad(b)) > 0) {
os.write(b, 0, i);
}
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
throw new BusinessException("代理⼈" + agentCode + "⽣成WORD⽂档出错", false);        }
}
private boolean Wordsave(String agentCode, HttpServletRequest request,
HttpServletResponse response) {
boolean Wordsave = false;
Map<String, Object> dataMap = new HashMap<String, Object>();
try {
//获取⼈员信息
PrpSagent prpSagent = PrpSagentByAgentCode(agentCode);
if (prpSagent != null) {
//将⼈员信息存⼊map集合
dataMap.put("name",
((AgentName() == null) ||
"".AgentName())) ? ""
: AgentName());
dataMap.put("sex",
((Sex() == null) ||
"".Sex())) ? "" : Sex());
dataMap.put("school",
((GraduateSchool() == null) ||
"".GraduateSchool())) ? ""
: GraduateSchool());
dataMap.put("educ",
((Education() == null) ||
"".Education())) ? ""
: Education());
dataMap.put("addressName",
((AddressName() == null) ||
"".AddressName())) ? ""
: AddressName());
: AddressName());
dataMap.put("idnumber",
((IdentifyNumber() == null) ||
"".IdentifyNumber())) ? ""
: IdentifyNumber());
dataMap.put("phone",
((PhoneNumber() == null) ||
"".PhoneNumber())) ? ""
: PhoneNumber());
Configuration configuration = new Configuration();
getsavefilename
configuration.setDefaultEncoding("utf-8");
String url = Resource("")
.toString();
//  file:/F:/Svn/20190219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring/                url = "/" + url.substring(6, url.indexOf("WEB-INF")) +
"downloadFiles";
//⽣成word存储某⼀路径
System.out.println(url);
//  /F:/Svn/20190219-myg/webapp/downloadFiles
configuration.setDirectoryForTemplateLoading(new File(url));
File outFile = new File(url + "/" + "agentInfo" + "/" +
agentCode + ".doc");
//获取word模板
Template template = Template("个代信息登记表.ftl",
"utf-8");
//往模板中写⼊数据
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"), 10240);
template.process(dataMap, out);
out.close();
Wordsave = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return Wordsave;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。