Java导出word文档

 

1、新建一个word文档, 输入如下内容:

(里面的Tb1开头就是我们需要替换的占位字段,这里的占位字段不要太复杂,比如${Tb123},因为后面另存为xml后,多种类型的字符会发生拆分)

2、将该word文件另存为xml格式(注意是另存为,不是直接改扩展名)

3、将xml文件的扩展名直接改为ftl

(在文本编辑器里面打开ftl模版。然后搜索在word里填充的占位符。然后替换成后面能被替换的格式。比如,把Tb111,替换成${tb111}。

这里可能原始的占位符发生了截断,比如本来是Tb111,但是在ftl模版里面成了Tb1~<dafda<11。这时就要把中间的那些删掉。最后替换后的格式就是标准的可替换参数)

 

添加依赖
<dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.28</version>
</dependency>
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class WordExportUtil {
    private static Logger LOGGER = LoggerFactory.getLogger(WordExportUtil.class);
    
    private static WordExportUtil service = null; 

    private WordExportUtil() {
        super();
    }
    
    public static WordExportUtil getInstance() {
        if(service == null) {
            synchronized(WordExportUtil.class){
                if(service == null) {
                    service = new WordExportUtil();
                }
            }
        }
        return service;
    }
    
    /**
     * 
     * @param templateFilePath  eg: /template/test/test.ftl
     * @param dataMap
     * @param exportFilePath  eg: /tmp/test/test123.doc
     * @param loadType  设置路径加载方式。1-绝对路径,2-项目相对路径
     * @return
     * @throws Exception 
     */
    public File createDocFile(String templateFilePath,Map<String, Object> dataMap, String exportFilePath, int loadType) throws Exception {
        Template t = null;
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setDefaultEncoding("UTF-8");
        try {
            templateFilePath = pathReplace(templateFilePath); 
            String ftlPath = templateFilePath.substring(0, templateFilePath.lastIndexOf("/"));
            if(loadType == 1) {
                configuration.setDirectoryForTemplateLoading(new File(ftlPath)); // FTL文件所存在的位置
            }else {
                configuration.setClassForTemplateLoading(this.getClass(), ftlPath);//以类加载的方式查找模版文件路径
            }
            

            String ftlFile = templateFilePath.substring(templateFilePath.lastIndexOf("/")+1);
            t = configuration.getTemplate(ftlFile); // 模板文件名
            
            File outFile = new File(exportFilePath);
            Writer out = null;
            
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
            
            t.process(dataMap, out);
        } catch (Exception e) {
            LOGGER.error("导出word文档出错", e);
            throw e;
        }
        return null;
    }
    
    /**
     *  把路径的\替换成/
     * @param path
     * @return
     */
    private String pathReplace(String path) {
        while(path != null && path.contains("\\")) {
            path = path.replace("\\", "/");
        }
        return path;
    }
}
public static void main(String[] args) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        getData(dataMap);
        String templateFile = "C:\\Users\\luke\\Desktop\\tmpStore\\exportTemplate.ftl";
        String exportFile = "C:\\Users\\luke\\Desktop\\tmpStore\\luedf.doc";
                
        try {
            WordExportUtil.getInstance().createDocFile(templateFile, dataMap, exportFile, 1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 测试用的
     * @param dataMap
     */
    public static void getData(Map<String, Object> dataMap) {
        dataMap.put("tb11", "10");
        dataMap.put("tb12", "2012");
        dataMap.put("tb13", "2");
        dataMap.put("tb111", "13");
        dataMap.put("tb112", "13");
        dataMap.put("tb113", "13");
        dataMap.put("tb114", "13");
        dataMap.put("tb115", "13");
        dataMap.put("tb116", "13");
        dataMap.put("tb117", "13");
        dataMap.put("tb118", "13");
        dataMap.put("tb119", "13");
        dataMap.put("tb1110", "13");
        dataMap.put("tb1111", "13");
    }

 

posted @ 2022-07-14 17:07  Dabo丶  阅读(1721)  评论(1编辑  收藏  举报