标签:position byte port request 效果 template actor 资料 bytes
最近公司来了个项目,需要把数据库数据查询,进行组装然后导出为doc文档。查阅了下网上资料,决定使用freeMaker来开发。
首先打开word,编辑文档。然后将文档另存为后缀为ftl的文件。
贴上代码:
@ApiImplicitParam(name = "exportReportWord", value = "检查报告导出", dataType = "ChkReportPageQuery")
@ApiOperation(value = "检查报告导出")
@PostMapping("/exportReportWord")
public void exportReportWord(HttpServletRequest request, HttpServletResponse response, @RequestBody ChkReportPageQuery pageQuery) throws IOException {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/msword");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=xx.doc");
String path = new File("").getAbsolutePath();
Map dataMap = new HashMap<>();
dataMap.put("userName","陈聪");
dataMap.put("frame","SpringCloud");
dataMap.put("tools","freeMarker");
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
//项目地址
configuration.setClassForTemplateLoading(this.getClass(),"/template");
Template template = null;
try{
template = configuration.getTemplate("/导出doc测试.ftl");
}catch (IOException e){
e.printStackTrace();
}
//输出文档路径和地址
File outFile = new File(path+"/export/wordExport.doc");
Writer out = null;
FileOutputStream fileOutputStream = null;
try{
fileOutputStream = new FileOutputStream(outFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
out = new BufferedWriter(outputStreamWriter);
}catch (FileNotFoundException fileExp){
fileExp.printStackTrace();
}
try{
template.process(dataMap,out);
out.close();
fileOutputStream.close();
//下载文件
DownLoadUtil.downLoadFile(path+"/export/wordExport.doc",response);
}catch (TemplateException tExp){
tExp.printStackTrace();
}
}
public class DownLoadUtil {
static Logger logger = LoggerFactory.getLogger(DownLoadUtil.class);
public static void downLoadFile(String fullPath, HttpServletResponse response) {
logger.info("【downLoadFile:fullPath】:==>" + fullPath);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//创建文件
File file = new File(fullPath);
String fileName = file.getName();
//读文件流
inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
//清空响应
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream; charset=utf-8");
// response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1"));
response.setHeader("Content-Length", "" + file.length());
//写文件流
outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(buffer);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
如果有循环的数据需要在模板中定义list,然后将list数据放进dataMap。如下:
至此SpringCloud使用freeMarker导出doc文件完成。
导出效果,不循环:
循环效果:
标签:position byte port request 效果 template actor 资料 bytes
原文地址:https://www.cnblogs.com/0116ct/p/14951007.html