java实现导出自定义word文档,使用freemarker

例子:

1、在word文档中设计好所需word风格,图片除外,要显示的值用${英文}代替;

d6a60c0efc1cd5cd917ff2943b38710e116.jpg

2、设计好后,另存为xml格式,保存;

e87fd3468368f469675bf4b630b6b2660b7.jpg

3、打开另存为的xml文件,图片是base64码,先把图片的base64码删除掉;

c0d3ac409a728bc0faaae154ffcf049daf6.jpg

4、删掉之后在浏览器找一个在线xml格式化,更方面操作,格式化后把后缀改成ftl放到项目中resources下面,最好在resources下面创建一个文件件,我的叫Freemarker,然后放下面;

6c59beb0c0ada7a4e541a561e500f100d5f.jpg

5、因为我要的数据不止一条可能有很多,那么就需要遍历;

ccd397e54d5daf6753e8cad9b4bfa5df6b8.jpg

6、下面上java代码;

maven依赖:

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

工具类:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * @Auther: zyx
 * @Date: 2018/8/7 11:12
 */
public final class DocUtils {

  /**
   * create one word document and Write data to the freemarker template
   *
   * @param: [dataMap, path, fileName, ftlPath, ftlName]
   * @return: void
   * @author: zyx
   * @date: 2018/8/10 14:37
   */
  public static void createWord(Map<String, Object> dataMap, String path, String fileName,
      String ftlPath, String ftlName) {

    if (dataMap == null || dataMap.size() == 0) {
      return;
    }

    try {
      Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
      configuration.setDefaultEncoding("utf-8");
      //Fixed physical hard drive location
      //configuration.setDirectoryForTemplateLoading(new File(path));
      //project static resources under folder location
      configuration.setClassForTemplateLoading(DocUtils.class, ftlPath);

      //output document path and name
      File outFile = new File(path+ "/" + fileName + ".doc");
      //utf-8 read ftl document
      Template t = configuration.getTemplate(ftlName, "utf-8");
      Writer out = new BufferedWriter(
          new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
      t.process(dataMap, out);
      out.close();

    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * get the images base64 code
   *
   * @param: [src]
   * @return: java.lang.String
   * @author: zyx
   * @date: 2018/8/10 14:36
   */
  public static String getImageBase(String src) {
    if (src == null || src == "") {
      return "";
    }
    File file = new File(src);
    if (!file.exists()) {
      return "";
    }
    InputStream in = null;
    byte[] data = null;
    try {
      in = new FileInputStream(file);
    } catch (FileNotFoundException e1) {
      e1.printStackTrace();
    }
    try {
      data = new byte[in.available()];
      in.read(data);
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    Encoder encoder = Base64.getEncoder();
    return encoder.encodeToString(data);
  }
}

 

测试类:

import static word.DocUtils.createWord;
import static word.DocUtils.getImageBase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zyx
 * @Date: 2018/8/14 18:56
 */
public class AlarmTest {

  public static void main(String[] args) {
    Map<String, Object> dataMap = handlerMap();
    String path = "D:/Export";
    String fileName = "test";
    String ftlPath = "/Freemarker";
    String ftlName = "alarm.ftl";

    createWord(dataMap, path, fileName, ftlPath, ftlName);
  }

  private static Map<String, Object> handlerMap() {
    List<Map<String, Object>> list = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
      Map<String, Object> dataMap = new HashMap<>();
      dataMap.put("cameraName", "hi看哈圣诞节佛诞节暗示法");
      dataMap.put("score", "95.89%");
      dataMap.put("inTime", "2018-08-14 19:04:56");
      dataMap.put("currentTime", "2018-08-14 19:04:56");
      dataMap.put("outTime", "2018-08-14 19:04:56");
      dataMap.put("status", "待复核");
      dataMap.put("sampleName", "王麻子");
      dataMap.put("nation", "汉族");
      dataMap.put("idNumber", "410326199408222312");
      dataMap.put("personType", "国际总裁");
      dataMap.put("libraryName", "好久好久");
      dataMap.put("address", "美国纽约");
      dataMap.put("detailAddress", "大佛山分公司");
      dataMap.put("extractPath", getImageBase("D:/Export/1.png"));
      dataMap.put("samplePath", getImageBase("D:/Export/2.png"));
      dataMap.put("snapshotPath", getImageBase("D:/Export/3.png"));
      list.add(dataMap);
    }

    Map<String, Object> map = new HashMap<>();
    map.put("recordList", list);

    return map;
  }

}

 

运行结果:

b2b82305331dfa2c455efa12b7bd509d62d.jpg

 

作者:yaxin zhang

转载于:https://my.oschina.net/zhangyaxin/blog/1929035

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值