|
@@ -28,7 +28,7 @@ public class WordUtil {
|
|
|
* @param fileName 要输出的文件路径
|
|
|
* @throws Exception 抛出的异常
|
|
|
*/
|
|
|
- public static void generateWord(Map<String, Object> dataMap, String templateName, String fileName) throws Exception {
|
|
|
+ public static void generateWord(Map<String, Object> dataMap, String templateName, String path, String fileName) throws Exception {
|
|
|
// 设置FreeMarker的版本和编码格式
|
|
|
Configuration configuration = new Configuration(new Version("2.3.28"));
|
|
|
configuration.setDefaultEncoding("UTF-8");
|
|
@@ -39,8 +39,13 @@ public class WordUtil {
|
|
|
configuration.setClassForTemplateLoading(WordUtil.class, "/templates");
|
|
|
// 设置FreeMarker生成Word文档所需要的模板
|
|
|
Template tem = configuration.getTemplate(templateName, "UTF-8");
|
|
|
+ // 查看路径下是否有存放文档的文件夹 没有就创建
|
|
|
+ File file = new File(path);
|
|
|
+ if (!file.exists()){
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
// 创建一个Word文档的输出流
|
|
|
- Writer out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(new File(fileName).toPath()), StandardCharsets.UTF_8));
|
|
|
+ Writer out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(new File(path + fileName).toPath()), StandardCharsets.UTF_8));
|
|
|
// FreeMarker使用Word模板和数据生成Word文档
|
|
|
tem.process(dataMap, out);
|
|
|
out.flush();
|