java ftl生成doc文档_通过freemarker生成word文档的问题

最近在开发一个功能,就是通过JAVA生成word文档,调研了很多开源框架,最终决定通过freemarker实现。具体实现方式如下:

先用word做个模板出来,然后保存为XML形式,再将需要替换的地方更换为freemarker标签,然后在JAVA中将模板读入在写出。

调试的时候,是用main方法弄的,没有任何问题,生成的word文档打开正常。

问题就出在将代码通过web形式调用时,JSP上点击按钮,然后调用代码,最后生成的word文档提示无法打开,系统无异常。

请问问题可能会出在哪块?

一下是部分代码:

public class DocumentHandler {

public static void createDoc(Map dataMap, String templateName, String wordPathName) {

Writer out = null;

try {

Configuration configuration = new Configuration();

configuration.setDefaultEncoding("utf-8");

System.out.println(configuration.getDefaultEncoding());

configuration.setClassForTemplateLoading(DocumentHandler.class, ResourcesUtil.getProperties(Constants.TEMPLATE_PATH));

// 加载模板

Template template = configuration.getTemplate(templateName);

File outFile = new File(wordPathName);

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));

template.process(dataMap, out);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (TemplateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (out != null) {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

调用方法:

Map dataMap = new HashMap();

dataMap.put("aaa", 111);

dataMap.put("bbb", "ddd");

dataMap.put("ccc", 33);

createDoc(dataMap, "test.ftl", "d:\\test.doc");

### 回答1: 可以使用以下代码,通过FreeMarker生成Word文档: Configuration cfg = new Configuration(); cfg.setClassForTemplateLoading(this.getClass(), "/"); Template t = cfg.getTemplate("word.ftl");StringWriter stringWriter = new StringWriter(); t.process(dataMap, stringWriter); InputStream is = new ByteArrayInputStream(stringWriter.toString().getBytes("utf-8")); OutputStream os = new FileOutputStream(new File("/word.doc")); POIFSFileSystem fs = new POIFSFileSystem();// 对应于org.apache.poi.hdf.extractor.WordDocument fs.createDocumentInputStream(is); fs.writeFilesystem(os); os.close(); ### 回答2: Java中使用FreeMarker生成Word文档的代码如下所示: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.*; import java.util.HashMap; import java.util.Map; public class WordGenerator { public static void main(String[] args) { // 设置模板文件路径 String templatePath = "path/to/template.ftl"; // 设置输出文件路径 String outputPath = "path/to/output.doc"; // 创建FreeMarker配置实例 Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); try { // 加载模板文件 cfg.setDirectoryForTemplateLoading(new File("path/to/templates")); Template template = cfg.getTemplate(templatePath); // 创建数据模型 Map<String, Object> data = new HashMap<>(); data.put("title", "Hello FreeMarker"); // 设置输出文件 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath), "UTF-8")); // 将模板和数据模型合并生成Word文档 template.process(data, out); // 关闭流 out.flush(); out.close(); System.out.println("Word文档生成成功!"); } catch (IOException | TemplateException e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先需要设置模板文件路径(templatePath)和输出文件路径(outputPath)。然后创建FreeMarker的Configuration实例,并设置模板文件所在目录。接下来,加载模板文件并创建数据模型,将数据模型和模板进行合并生成Word文档。最后,关闭输出流并打印生成成功的提示信息。 在使用这段代码之前,需要确保已经添加了FreeMarker的依赖库。可以通过在pom.xml文件中添加以下依赖实现: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> ``` 以上就是使用FreeMarker生成Word文档的代码及相关说明。 ### 回答3: 生成word文档可以使用FreeMarker这个开源的模板引擎库结合Java代码来实现。下面是一个示例代码,用于生成word文档: 1. 首先,确保你已经下载并在项目中引入了FreeMarker的相关Jar文件。 2. 创建一个用于存储模板文件(.ftl)的文件夹,并在该文件夹下创建一个word模板文件,例如template.ftl。在该模板中,你可以定义word文档的样式和内容。 3. 在Java代码中引入FreeMarker的相关类和包,例如freemarker.template.Configuration、freemarker.template.Template和java.io包。 4. 初始化FreeMarker的Configuration对象,并设置模板文件所在的路径。 5. 使用Configuration对象创建一个Template对象,通过加载模板文件。 6. 创建一个用于输出word文档的Writer对象,例如OutputStreamWriter或FileWriter。 7. 使用Template对象的process方法,传入模板所需的数据和输出Writer对象,来生成word文档。 下面是一个简单的示例代码: ``` import freemarker.template.Configuration; import freemarker.template.Template; import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; public class FreeMarkerWordGenerator { public static void main(String[] args) { try { Configuration configuration = new Configuration(Configuration.VERSION_2_3_30); configuration.setDirectoryForTemplateLoading(new File("模板文件夹路径")); configuration.setDefaultEncoding("UTF-8"); Template template = configuration.getTemplate("template.ftl"); Map<String, Object> data = new HashMap<>(); data.put("title", "示例标题"); data.put("content", "示例内容"); Writer writer = new FileWriter("生成word文档路径"); template.process(data, writer); writer.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中的"模板文件夹路径"和"生成word文档路径"需要替换为你自己的路径。 这个示例代码会根据你的模板文件(template.ftl生成一个word文档,其中标题和内容使用了模板中定义的变量。你可以根据自己的需求来修改模板内容和生成的数据,来生成符合你需要的word文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值