JAVA中使用freemarker导出word的简单实现

2025-11-03 13:16:04

1、先做一个普通word模板,然后改成xm,然后将制定地方换成${title}之类表达式,然后换成ftl后缀

JAVA中使用freemarker导出word的简单实现

2、写一个工具类,代码如下

package com.zxw.user.util;

    import java.io.BufferedWriter; 

    import java.io.File; 

    import java.io.FileNotFoundException; 

    import java.io.FileOutputStream; 

    import java.io.IOException; 

    import java.io.OutputStreamWriter; 

    import java.io.UnsupportedEncodingException; 

    import java.io.Writer; 

    import java.util.Map; 

     

    import freemarker.template.Configuration; 

    import freemarker.template.Template; 

    import freemarker.template.TemplateException; 

     

    public class DocumentHandler { 

     

        private Configuration configuration = null; 

     

        public DocumentHandler() { 

            configuration = new Configuration(); 

            configuration.setDefaultEncoding("utf-8"); 

        } 

     

        public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException { 

            //dataMap 要填入模本的数据文件 

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

            //这里我们的模板是放在template包下面 

            configuration.setClassForTemplateLoading(this.getClass(),"/com/zxw/user/template"); 

            Template t=null; 

            try { 

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

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

            } catch (IOException e) { 

                e.printStackTrace(); 

            } 

            //输出文档路径及名称 

            File outFile = new File(fileName); 

            Writer out = null; 

            FileOutputStream fos=null; 

            try { 

                fos = new FileOutputStream(outFile); 

                OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8"); 

                //这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解析。 

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

                 out = new BufferedWriter(oWriter);  

            } catch (FileNotFoundException e1) { 

                e1.printStackTrace(); 

            } 

              

            try { 

                t.process(dataMap, out); 

                out.close(); 

                fos.close(); 

            } catch (TemplateException e) { 

                e.printStackTrace(); 

            } catch (IOException e) { 

                e.printStackTrace(); 

            } 

             

            //System.out.println("---------------------------"); 

        } 

}

3、main方法测试

public static void main(String[] args) throws UnsupportedEncodingException {

        //创建一个数据集合存入相应的值

            Map<String, Object> dataMap = new HashMap<String, Object>(); 

            dataMap.put("userName", "哈哈哈哈哈");

            dataMap.put("title", "关于这个插件WORD的测试工作进展");

            dataMap.put("text", "这个插件WORD的测试工作正在积极测试当中!!!!!!!!!!!!!!!!!!"); 

            //写入doc创建文件

            DocumentHandler doc=new DocumentHandler();

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

            System.out.println("doc创建成功!");

        }

到此,可以去d盘去查看已经创建的word文件,是不是参数已经填到相应位置了?

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢