• 【Java】导出word文档之freemarker导出


    Java导出word文档有很多种方式,本例介绍freemarker导出,根据现有的word模板进行导出

    一、简单导出(不含循环导出)

      1、新建一个word文件。如下图:
        

      2、使用word将文件另存为xml的格式
        

      3、编辑xml文件内容,将'用户名'替换成-> ${username}、'简介'替换成-> ${resume}、将图片内容用变量-> ${img}替换。
        
        --》

      4、修改xml文件后缀名,将xml修改为ftl格式。
        

      5、使用java代码,完成word文件导出,需要使用到freemarker.jar包,maven依赖如下:

    1 <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
    2 <dependency>
    3     <groupId>org.freemarker</groupId>
    4     <artifactId>freemarker</artifactId>
    5     <version>2.3.23</version>
    6 </dependency>
     1 package com.test.word;
     2 
     3 import java.io.BufferedWriter;
     4 import java.io.File;
     5 import java.io.FileInputStream;
     6 import java.io.FileNotFoundException;
     7 import java.io.FileOutputStream;
     8 import java.io.IOException;
     9 import java.io.InputStream;
    10 import java.io.OutputStreamWriter;
    11 import java.io.UnsupportedEncodingException;
    12 import java.io.Writer;
    13 import java.util.HashMap;
    14 import java.util.Map;
    15 
    16 import freemarker.template.Configuration;
    17 import freemarker.template.Template;
    18 import freemarker.template.TemplateException;
    19 import sun.misc.BASE64Encoder;
    20 
    21 public class Test {
    22 
    23     public static void main(String[] args) throws IOException, TemplateException {
    24 
    25         // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
    26         Map<String, String> dataMap = new HashMap<String, String>();
    27         dataMap.put("username", "张三");
    28         dataMap.put("resume", "我是谁?");
    29         dataMap.put("img", getImageStr());
    30 
    31         // Configuration用于读取ftl文件
    32         Configuration configuration = new Configuration();
    33         configuration.setDefaultEncoding("utf-8");
    34 
    35         /*
    36          * 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
    37          */
    38         // 指定路径的第一种方式(根据某个类的相对路径指定)
    39         // configuration.setClassForTemplateLoading(this.getClass(),"");
    40 
    41         // 指定路径的第二种方式,我的路径是C:/a.ftl
    42         configuration.setDirectoryForTemplateLoading(new File("C:/Users/H__D/Desktop/"));
    43 
    44         // 输出文档路径及名称
    45         File outFile = new File("C:/Users/H__D/Desktop/test.doc");
    46 
    47         // 以utf-8的编码读取ftl文件
    48         Template t = configuration.getTemplate("简历.ftl", "utf-8");
    49         Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
    50         t.process(dataMap, out);
    51         out.close();
    52 
    53     }
    54     
    55     
    56     /**
    57      * 将图片转换成base64编码
    58      * @return
    59      */
    60     public static String getImageStr() {  
    61          String imgFile = "C:/Users/H__D/Desktop/IMG_0109.JPG";  
    62          InputStream in = null;  
    63          byte[] data = null;  
    64          try {  
    65              in = new FileInputStream(imgFile);  
    66              data = new byte[in.available()];  
    67              in.read(data);  
    68              in.close();  
    69          } catch (Exception e) {  
    70              e.printStackTrace();  
    71          }  
    72          BASE64Encoder encoder = new BASE64Encoder();  
    73          return encoder.encode(data);  
    74      }  
    75 
    76 }

      6、打开test.doc,如下:
        

    二、带循环导出   

      1、新建一个带循环的word 文件,如下:
        

      2、使用word将文件另存为xml的格式

      3、编辑xml文件内容,用<#list userList as user> </#list>标签将循环标签包围起来(userList是集合的key, user是集合中的每个元素, 类似<c:forEach items='userList' var='user'>), 如图:
        

      4、修改xml文件后缀名,将xml修改为ftl格式。

      5、使用java代码,完成word文件导出,如下:

     1 package com.test.word;
     2 
     3 import java.io.BufferedWriter;
     4 import java.io.File;
     5 import java.io.FileInputStream;
     6 import java.io.FileNotFoundException;
     7 import java.io.FileOutputStream;
     8 import java.io.IOException;
     9 import java.io.InputStream;
    10 import java.io.OutputStreamWriter;
    11 import java.io.UnsupportedEncodingException;
    12 import java.io.Writer;
    13 import java.util.ArrayList;
    14 import java.util.HashMap;
    15 import java.util.List;
    16 import java.util.Map;
    17 
    18 import freemarker.template.Configuration;
    19 import freemarker.template.Template;
    20 import freemarker.template.TemplateException;
    21 
    22 public class Test2 {
    23 
    24     public static void main(String[] args) throws IOException, TemplateException {
    25 
    26         // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
    27         Map<String, List> dataMap = new HashMap<String, List>();
    28     
    29         List<User> list = new ArrayList<User>();  
    30         for(int i=0;i<5;i++){  
    31             User user = new User();  
    32             user.setName("hd"+(i+1));  
    33             list.add(user);  
    34         }  
    35         dataMap.put("userList", list); 
    36 
    37         // Configuration用于读取ftl文件
    38         Configuration configuration = new Configuration();
    39         configuration.setDefaultEncoding("utf-8");
    40 
    41         /*
    42          * 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
    43          */
    44         // 指定路径的第一种方式(根据某个类的相对路径指定)
    45         // configuration.setClassForTemplateLoading(this.getClass(),"");
    46 
    47         // 指定路径的第二种方式,我的路径是C:/a.ftl
    48         configuration.setDirectoryForTemplateLoading(new File("C:/Users/H__D/Desktop/"));
    49 
    50         // 输出文档路径及名称
    51         File outFile = new File("C:/Users/H__D/Desktop/test2.doc");
    52 
    53         // 以utf-8的编码读取ftl文件
    54         Template t = configuration.getTemplate("循环.ftl", "utf-8");
    55         Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
    56         t.process(dataMap, out);
    57         out.close();
    58 
    59     }
    60     
    61     
    62 
    63 }
     1 package com.test.word;
     2 
     3 public class User {
     4     private String name;
     5 
     6     public String getName() {
     7         return name;
     8     }
     9 
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     
    14     
    15 }

      6、打开test2.doc,如下:
        

        

     

     

     

  • 相关阅读:
    快速收录方法
    .NET学习网址大全,不得不上,国内外经典网站
    首篇文章测试。
    DropDownList的用法
    SqlServer初级学习笔记
    GDI编程开发
    C#继承细谈
    web开发的一些小零碎知识点(一)
    Js实现全选和批量删除
    IEnumberable和IEnumberator理解
  • 原文地址:https://www.cnblogs.com/h--d/p/7646976.html
Copyright © 2020-2023  润新知