简单介绍
在项目中经常会遇到报表相关的需求,而大多数会以excel出现,今天咱们说说word导出的一些事
文章目录
目录
简单介绍
freemarker+ftl
xdocreport开源工具
一、freemarker导出word步骤
二、xdocreport导出word步骤
三、常见语法介绍
总结
前言
Java对word导出支持不太友好,由于本人水平有限,知道两种方式生成word文件。
1.freemarker+ftl
2.xdocreport开源工具
freemarker+ftl
引入freemarker包,准备好需要生成word模板的ftl文件。该文件是有word生成xml,在由xml修改后缀名得到的temp.ftl文件
org.springframework.boot spring-boot-starter-freemarker
缺点:word —>xml —>ftl 的过程繁琐且容易出错,ftl为标签内容不易读,如果模板变更又要重复word —>xml —>ftl 的过程。需要强大的内心和耐心。
xdocreport开源工具
该开源工具填写word模板语法一致,但不需要生成ftl文件作为模板,本身word就可直接作为导出模板。
fr.opensagres.xdocreport fr.opensagres.xdocreport.template.freemarker2.0.2 fr.opensagres.xdocreport fr.opensagres.xdocreport.document.docx2.0.2
优点:可读性提高,修改模板是可以增量操作,无需复杂操作再次生成ftl文件
一、freemarker导出word步骤
- 新建word文档,写入内容样式,然后将动态获取的值放置内容展示区域
- 将文件保存后,另存为xml格式
- 打开文档显示内容如下,修改xml文件后缀名为ftl文件即可。
-
private void exportDoc(String fileName, HttpServletResponse response, String tempName) {
PrintWriter writer = null;
Map dataMap = new HashMap<>();
// 将要写入模板的数据塞到dataMap中。
dataMap.put("name":"i am kobe");
try {
Configuration configuration = new Configuration(new Version("2.3.0"));
configuration.setDefaultEncoding("utf-8");
// 上面配置模板的路径
configuration.setClassForTemplateLoading(DocServiceImpl.class, "/templates");
configuration.setTemplateLoader(new ClassTemplateLoader(DocServiceImpl.class, "/templates"));
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename="" + new String(fileName.getBytes("GBK"), "ISO-8859-1") + """);
response.setCharacterEncoding("utf-8");
writer = response.getWriter();
// tempName是模板的文件名称
Template template = configuration.getTemplate(tempName, "utf-8");
template.process(dataMap, writer);
} catch (Exception e) {
logger.error("导出word文档异常 : {}", e);
} finally {
writer.flush();
writer.close();
}
} 这样就完成了freemarker配置以及下载word文档的功能了。
二、xdocreport导出word步骤
- 还是之前那个文档,同样的属性 name字段,之前是${name} 现在变成 word加域的形式
- 如何加域,请看图片
- 点击上方缺点即可。模板已经配置完成了,是不是方便了许多呢?
- 上Java代码,直接撸代码了。
-
public void download(HttpServletRequest request,HttpServletResponse response){
try{
InputStream inputStream = new FileInputStream(new File("D:\template.docx"));
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
IContext context = report.createContext();
report.setFieldsmetadata(fieldsmetadata);
//替换word模板中创建的域的变量
context.put("name", "我叫张三");
//导出word
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// 获取OutputStream 也就是写入bout
report.process(context, bout);
//创建文件输出流
FileOutputStream out = new FileOutputStream("d:\daochudeword.docx");
out.write(bout.toByteArray());
out.close();
bout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
三、常见语法介绍
private void exportDoc(String fileName, HttpServletResponse response, String tempName) {
PrintWriter writer = null;
Map dataMap = new HashMap<>();
// 将要写入模板的数据塞到dataMap中。
dataMap.put("name":"i am kobe");
try {
Configuration configuration = new Configuration(new Version("2.3.0"));
configuration.setDefaultEncoding("utf-8");
// 上面配置模板的路径
configuration.setClassForTemplateLoading(DocServiceImpl.class, "/templates");
configuration.setTemplateLoader(new ClassTemplateLoader(DocServiceImpl.class, "/templates"));
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename="" + new String(fileName.getBytes("GBK"), "ISO-8859-1") + """);
response.setCharacterEncoding("utf-8");
writer = response.getWriter();
// tempName是模板的文件名称
Template template = configuration.getTemplate(tempName, "utf-8");
template.process(dataMap, writer);
} catch (Exception e) {
logger.error("导出word文档异常 : {}", e);
} finally {
writer.flush();
writer.close();
}
} 这样就完成了freemarker配置以及下载word文档的功能了。
- 还是之前那个文档,同样的属性 name字段,之前是${name} 现在变成 word加域的形式
- 如何加域,请看图片
- 点击上方缺点即可。模板已经配置完成了,是不是方便了许多呢?
- 上Java代码,直接撸代码了。
-
public void download(HttpServletRequest request,HttpServletResponse response){ try{ InputStream inputStream = new FileInputStream(new File("D:\template.docx")); IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker); IContext context = report.createContext(); report.setFieldsmetadata(fieldsmetadata); //替换word模板中创建的域的变量 context.put("name", "我叫张三"); //导出word ByteArrayOutputStream bout = new ByteArrayOutputStream(); // 获取OutputStream 也就是写入bout report.process(context, bout); //创建文件输出流 FileOutputStream out = new FileOutputStream("d:\daochudeword.docx"); out.write(bout.toByteArray()); out.close(); bout.close(); } catch (Exception e) { e.printStackTrace(); } }
三、常见语法介绍
使用才是开始,语法才是修行。xdocreport的语法是兼容freemarker的。
if判断的使用,下面是判断list集合是否为空且长度是否大于0
[#if list?? && (list?size>0)]
[#else]
list 集合为空
[/#if]
[#if list?? && (list?size>0)]
[/#if]
对象判空
[#if obj??] [/#if]
数组的使用,循环输出多条数据
[#list list as temp]
${temp}
[/#list]
总结
很多语法不是很常用,if和list是本人在项目中运用的较多的,后续会及时更新相关语法。



