java 导出word
来源:互联网 发布:云盘服务器架设php 编辑:程序博客网 时间:2024/06/05 04:31
导入jar
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
这里注意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
- java导出word
- Java 导出word文件
- java导出word;
- java 导出word文档
- Java导出word
- Java导出Word文档
- Java导出word
- java导出word模板
- Java导出word
- JAVA FreeMarker导出word
- java导出word文档
- JAVA WEB 导出 Word
- java导出word(Freemarker)
- java导出word模板
- Java 导出 Word 文档
- java word导出
- java导出word打不开
- java 导出word
- Redis 基础结构
- 使用halcon(版本10.0 64位)读入图像并进行显示,使用ChartAxis类实现图像直方图的显示?
- Head First 设计模式 之 oo设计原则简介
- Xcode8 打包上传构建版本不显示
- hadoop搭建
- java 导出word
- 1092:回文数1
- Swift 方法
- [LeetCode-Algorithms-7] "Reverse Integer" (2017.9.14-WEEK2)
- 关于重载全局new/delete 的一些问题
- Vue项目,当图片加载错误时,用默认图片代替
- 用CSS自定义浏览器滚动条
- 2017CVPR论文---Annotating object instances with a polygon -RNN
- js点击事件的时候浏览器报错,提示Uncaught TypeError: Cannot set property 'onclick' of null