导出文档
- 第一步就是将World文档里面需要从数据库填充的部分用占位符替换
- 第二步:就是将此文档保存为Xml格式
- 第四步:将其放在resource目录下,并选中此文件,右键点击properties属性,将其编码格式设置为Utf-8(防止生成之后乱码)
- 第四步:将此文件后缀名改为ftl
导入依赖
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.23version>
dependency>
编写配置
word:
ftlInheritorName: application.ftl
wordInheritorName: 申报文档
templatePath: E:\warehouse\文化图书馆\whtsg\src\main\resources\ftl
wordProjectName: 申报书
ftlDeclarationName: declaration.ftl
代码层
@Autowired
private Environment env;
@Override
public void derive(HttpServletRequest request, HttpServletResponse response,String id) throws Exception {
HeritageProjectVo heritageProjectVo = getById(id);
Map<String, Object> map = new HashMap<>();
map.put("proCode",HeritageProjectEnum.ProCode.getName(heritageProjectVo.getProCode()));
map.put("category",HeritageProjectEnum.Category.getName(heritageProjectVo.getCategory()));
map.put("proName", heritageProjectVo.getProName());
map.put("recUnit", heritageProjectVo.getDirectorDept());
map.put("declareArea", heritageProjectVo.getProAddress());
map.put("involve", heritageProjectVo.getInvolve());
map.put("proSituation", heritageProjectVo.getProIntro());
map.put("basicComponents", heritageProjectVo.getHeritageInfo());
map.put("distributionArea", heritageProjectVo.getRegion());
map.put("historicalOrigin", heritageProjectVo.getHistory());
map.put("inheritAncestry", heritageProjectVo.getInheritSpec());
map.put("inheritGroup", heritageProjectVo.getInheritPeople());
wordUtils.exportMillCertificateWord(request,response,map,env.getProperty("word.wordProjectName"),env.getProperty("word.ftlDeclarationName"),env.getProperty("word.templatePath"));
}
编写工具类
package com.sykj.why.util;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.stereotype.Service;
@Service
public class WordUtils {
private static Configuration configuration = null;
private String templateFolder;
private boolean connection(HttpServletRequest request, String templatePath) {
try {
if (templateFolder == null) {
templateFolder = templatePath;
}
configuration = new Configuration(Configuration.VERSION_2_3_0);
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File(templateFolder));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile, String templatePath) throws IOException {
if (connection(request, templatePath) == false) {
return;
}
Template freemarkerTemplate = configuration.getTemplate(ftlFile);
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
file = createDoc(title, map, freemarkerTemplate);
fin = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
String fileName = title + ".doc";
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[512];
int bytesToRead = -1;
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} finally {
if (fin != null) fin.close();
if (out != null) out.close();
if (file != null) file.delete();
}
}
private File createDoc(String name, Map<?, ?> dataMap, Template template) {
File f = new File(name);
Template t = template;
try (
Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
) {
t.process(dataMap, w);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return f;
}
}