xml文件转为ftl_Freemarker + xml 实现Java导出word

前言

最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选择功能强大的freemarker+固定格式之后的wordxml实现导出功能。导出word的代码是可以直接复用的,于是在此贴出,并进行总结,方便大家拿走。

实现过程概览

先在word上,调整好自己想要的样子。然后存为xml文件。保存为freemarker模板,以ftl后缀结尾。将需要替换的变量使用freemarker的语法进行替换。最终将数据准备好,和模板进行渲染,生成文件并返回给浏览器流。

详细的实现过程

准备好word的样式

我们新建一个word,我们应该使用Microsoft office,如果使用wps可能会造成样式有些不兼容。在新建的office中,设置好我们的表格样式。我们的调查问卷涉及到四种类型,单选,多选,填空,简答。我们做出四种类型的示例。

样式没有问题后,我们选择另存为word xml 2003版本。将会生成一个xml文件。

格式化xml,并用freemarker语法替换xml

我们可以先下载一个工具 firstobject xml editor,这个可以帮助我们查看xml,同时方便我们定位我们需要改的位置。

复制过去之后,按f8可以将其进行格式化,左侧是标签,右侧是内容,我们只需要关注w:body即可。

像右侧的调查问卷这个就是个标题,我们实际渲染的时候应该将其进行替换,比如我们的程序数据map中,有title属性,我们想要这里展示,我们就使用语法${title}即可。

freemarker的具体语法,可以参考freemarker的问题,在这里我给出几个简单的例子。

比如我们将所有的数据放置在dataList中,所以我们需要判断,dataList是不是空,是空,我们不应该进行下面的逻辑,不是空,我们应该先循环题目是必须的,答案是需要根据类型进行再次循环的。语法参考文档,这里不再赘述。

程序端引入freemarker

org.freemarker

freemarker

将我们的flt文件放在resources下的templates下。

后端代码实现

此代码可以复用,在此贴出

public class WordUtils {

private static Configuration configuration = null;

private static final String templateFolder = WordUtils.class.getClassLoader().getResource("").getPath()+"/templates/word";

static {

configuration = new Configuration();

configuration.setDefaultEncoding("utf-8");

try {

configuration.setDirectoryForTemplateLoading(new File(templateFolder));

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* @Description:导出word,传入request,response,map就是值,title是导出问卷名,ftl是你要使用的模板名

*/

public static void exportWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile) throws Exception {

Template freemarkerTemplate = configuration.getTemplate(ftlFile);

File file = null;

InputStream fin = null;

ServletOutputStream out = null;

try {

file = createDocFile(map,freemarkerTemplate);

fin = new FileInputStream(file);

String fileName = title + ".doc";

response.setCharacterEncoding("utf-8");

response.setContentType("application/msword");

response.setHeader("Content-Disposition", "attachment;filename="

+fileName);

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();

}

}

/**

* @Description:创建doc文件

*/

private static File createDocFile(Map, ?> dataMap, Template template) {

File file = new File("init.doc");

try {

Writer writer = new OutputStreamWriter(new FileOutputStream(file), "utf-8");

template.process(dataMap, writer);

writer.close();

} catch (Exception e) {

e.printStackTrace();

}

return file;

}

}

有了工具类后,我们准备好我们的map数据。map里面的数据大家可以自行定义。然后调用utils中的导出方法即可。

WordUtils.exportWord(request, response, dataMap, "word", "demo.ftl");

结语

至此已经结束了,十分的好用,有疑问的话,可以评论交流。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Java中的Freemarker模板导出Word文件时,默认情况下,文件的格式是由文件名后缀决定的。如果你想将其另存为doc格式而不是xml格式,你可以尝试以下方法: 1. 确保文件名的后缀为.doc或.docx。在保存文件时,将文件名设置为以.doc或.docx结尾,如"example.doc"或"example.docx"。 2. 确保使用的是正确的输出流。在保存文件时,确保你使用正确的输出流类型来保存Word文件。例如,使用FileOutputStream来保存文件。 以下是一个简单的示例代码,展示了如何使用Freemarker模板导出Word文件并将其另存为doc格式: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.*; public class WordExport { public static void main(String[] args) { Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); configuration.setDefaultEncoding("UTF-8"); configuration.setClassForTemplateLoading(WordExport.class, "/templates"); try { Template template = configuration.getTemplate("template.ftl"); File outputFile = new File("output.doc"); Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); template.process(dataModel, writer); writer.close(); } catch (IOException | TemplateException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们将文件名设置为"output.doc",这样保存的就是doc格式的Word文件。 请确保你在代码中适当地替换模板路径、数据模型和文件名,以适应你的具体情况。 希望这能解决你的问题!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值