leftso 904 7 2019-05-15 17:49:07

Java word模板导出easypoi篇

一、maven依赖

        <!--easypoi  自动POI依赖版本为3.15-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!--注意:word中要使用循环等标签必须单独导入以下依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.1</version>
        </dependency>

二、word模板制作

word模板
word模板

提示:某些时候写的表达式感觉总是不能被替换为值,试着把表达式复制到文本工具里面再复制回来。

三、工具类
package net.ifok.demo.easypoiword;

import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordUtil {

    /**
     * 导出word(2007版本docx)
     *
     * @param templateWordPath
     * @param dataMap
     * @return
     * @throws Exception
     */
    public static byte[] exportWord(String templateWordPath, Map<String, Object> dataMap) throws Exception {
        File tf = new File(templateWordPath);
        if (!tf.exists() || !tf.isFile()) {
            throw new RuntimeException("File [" + templateWordPath + "] Not Found Or Not File.");
        }
        XWPFDocument document = WordExportUtil.exportWord07(templateWordPath, dataMap);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        document.write(bos);
        return bos.toByteArray();
    }

    public static void main(String[] args) {
        try {
            try (FileOutputStream fos = new FileOutputStream("d:/test/exp.docx")) {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("title", "个人信息");
                Map<String, String> user = new HashMap<>();
                user.put("name", "张三");
                user.put("age", "22");
                user.put("address", "重庆渝北区");
                user.put("other", "篮球");
                dataMap.put("user", user);

                List<Map<String, String>> jobs = new ArrayList<>();
                Map<String, String> job;
                for (int i = 0; i < 5; i++) {
                    job = new HashMap<>();
                    job.put("name", "公司名称-" + i);
                    job.put("address", "公司地址:" + i);
                    jobs.add(job);
                }

                dataMap.put("jobs",jobs);

                byte[] doc = exportWord("d:/test/template.docx", dataMap);

                fos.write(doc);

                fos.flush();

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

四、执行工具类测试


结果图:
导出结果图
导出结果图


 
编程技术 easypoi word java