DocumentHandler-ftl生成word

 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.Map;

import freemarker.template.Configuration;

import freemarker.template.Template;

 

public class DocumentHandler {

         private Configuration configuration = null;

 

         /**

          * 创建configuration

          * @param ftlSrc  模版存放路径,ftl所在的文件夹路径

          */

         public DocumentHandler(String ftlSrc) {

                   configuration = new Configuration();

                   configuration.setDefaultEncoding("utf-8");

                   try {

                            configuration.setDirectoryForTemplateLoading(new File(ftlSrc));

                   } catch (IOException e) {

                            e.printStackTrace();

                   }

         }

        

         /**

          * 根据ftl生成word方法

          * @param dataMap   -- 数据集

          * @param fileSrc   -- 生成后的文件地址

          * @param ftlName   -- ftl模版名称

          */

         public void createOrderAndDown(Map<String,Object> dataMap,String fileSrc,String ftlName) {

                   Template t=null;

                   try {

                            //test.ftl为要装载的模板

                            t = configuration.getTemplate(ftlName);

                           

                   } catch (IOException e) {

                            e.printStackTrace();

                   }

                   //输出文档路径及名称

                   File outFile = new File(fileSrc);

                    //如果输出目标文件夹不存在,则创建

        if (!outFile.getParentFile().exists()){

            outFile.getParentFile().mkdirs();

        }

                   Writer out = null;

                   try {

                            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));

                 //生成

                            t.process(dataMap, out);

                  

                   } catch (Exception e) {

                            e.printStackTrace();

                   }finally {

                            if(out!=null){

                                     //关闭流

                     try {

                              out.flush();

                                               out.close();

                                     } catch (IOException e) {

                                               e.printStackTrace();

                                     }

                            }

                   }       

         }

         /**

          * 要把模版生成合同

          * @param dataMap   数据map集合

          * @param fileSrc   生成后的文件路径

          */

         public void createDoc(Map<String,Object> dataMap,String fileSrc) {

                   //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,

                   //这里我们的模板是放在/com/zhiwei/credit/util/xmlToWord/firstCreditor包下面

         //      configuration.setClassForTemplateLoading(this.getClass(), "/com/zhiwei/credit/util/xmlToWord/firstCreditor");

         //      configuration.setServletContextForTemplateLoading(ServletActionContext.getContext(), "WEB-INF/templates");

         //      configuration.setDirectoryForTemplateLoading(arg0);

                   Template t=null;

                   try {

                            //test.ftl为要装载的模板

                            t = configuration.getTemplate("firstCreditor.ftl");

                   } catch (IOException e) {

                            e.printStackTrace();

                   }

                   //输出文档路径及名称

                   File outFile = new File(fileSrc);

                    //如果输出目标文件夹不存在,则创建

        if (!outFile.getParentFile().exists()){

            outFile.getParentFile().mkdirs();

        }

                   Writer out = null;

                   try {

                            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));

                 //生成

                            t.process(dataMap, out);

                   } catch (Exception e) {

                            e.printStackTrace();

                   }finally {

                            if(out!=null){

                                     //关闭流

                     try {

                              out.flush();

                                               out.close();

                                     } catch (IOException e) {

                                               e.printStackTrace();

                                     }

                            }

                   }

         }

        

 

}

posted on   我是司  阅读(671)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示