java 导出word

导入jar

    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
    </dependency>

word模版

ftl
生成word
word转xml 通过浏览器来替换字段,然后保存为flt格式即可

javascript:document.body.contentEditable='true';document.designMode='on'; void 0

这里注意ftl中的语法 可以参考 https://hran.me/archives/freemarker.html/comment-page-3#menu_index_9

java 代码

  @GetMapping(value = "/createUserListWord")
  public ResponseEntity<Void> createUserListWord() {
    fileService.createUserListWord();
    return ResponseEntity.ok().build();
  }
 @Override
  public void createUserListWord() {
    Map<?, ?> root = initData();  //数据源对象
    String template = "/templates/list1.ftl";  //模板文件的地址
    String path = "E:\\list1.doc";  //生成的word文档的输出地址
    WordUtil.process(root, template, path);
  }
  private Map<?, ?> initData() {
    Map<String, Object> root = new HashMap<String, Object>();
    List<User> users = new ArrayList<User>();
    List<String> list = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    map.put("11", "林陈策");
    map.put("22", "林陈策");
    list.add("□");
    list.add("□");
    list.add("√");
    list.add("□");
    User zhangsan = new User("张三", "12", list, map);
    User lisi = new User("李四", "12", list, map);
    users.add(zhangsan);
    users.add(lisi);

    root.put("users", users);
    root.put("title", "用户列表");
    return root;
  }
/**
 * Created by lcc on 2017/9/13.
 */

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.Map;

public final class WordUtil {
  private static Configuration configuration = null;

  private WordUtil() {
    throw new AssertionError();
  }

  /**
   * 根据模板生成相应的文件
   *
   * @param root     保存数据的map
   * @param template 模板文件的地址
   * @param path     生成的word文档输出地址
   * @return
   */
  public static synchronized File process(Map<?, ?> root, String template, String path) {

    if (null == root) {
      throw new RuntimeException("数据不能为空");
    }

    if (null == template) {
      throw new RuntimeException("模板文件不能为空");
    }

    if (null == path) {
      throw new RuntimeException("输出路径不能为空");
    }

    File file = new File(path);
    String templatePath = template.substring(0, template.lastIndexOf("/"));
    String templateName = template.substring(template.lastIndexOf("/") + 1, template.length());

    if (null == configuration) {
      configuration = new Configuration(Configuration.VERSION_2_3_23);  // 这里Configurantion对象不能有两个,否则多线程访问会报错
      configuration.setDefaultEncoding("utf-8");
      configuration.setClassicCompatible(true);
    }
    configuration.setClassForTemplateLoading(WordUtil.class, templatePath);

    Template t = null;
    try {
      t = configuration.getTemplate(templateName);
      Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
      t.process(root, w);  // 这里w是一个输出地址,可以输出到任何位置,如控制台,网页等
      w.close();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return file;
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值