java使用freemarker操作word

一.介绍

这一天公司安排,让我套用模板,数据库转存到word,还要按照给定的模板进行套用
在这里插入图片描述

二.准备

1.导入依赖

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

2.先看word文档

这里我新建一个A.docx的word文档进行测试,内容如下(因为有的模板涉及到图片,这里就也加一个图片)文字模板要是${}格式的
在这里插入图片描述
如果是offic的可以按住F12然后另存为xml格式的文件在这里插入图片描述
xml文件的代码如果没有结构可以去菜鸟进行格式化
菜鸟格式化地址:https://c.runoob.com/front-end/710/
格式化玩进行查看,有的人这里格式会不对注意一下(最好的就是在word写一个好的注释,然后换成xml之后在进行加上${}的格式)
在这里插入图片描述
下面看图片,搜索png,会看到下面的byte码,这边修改一下就可以(把<pkg:binaryData></pkg:binaryData>里面的内容图换位占位符就可以了)
在这里插入图片描述在这里插入图片描述然后保存,将文件的后缀改为.ftl

三.代码

1.这里为了方便我就直接在启动类进行写了,后期对接数据库什么的直接赋值就可,这里直接写死先

public class TestApplication {
    
    

    private static String Path = "C:\\picture\\";

    public static void main(String[] args) {
    
    

//        SpringApplication.run(TestApplication.class, args);
        importWord();
    }

    public static void importWord() {
    
    
        try {
    
    
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("test", new SimpleDateFormat("yyyy年MM月dd日").format(new Date()));
            dataMap.put("img", getImageUrlStr(Path + "a.jpg"));
//            dataMap.put("img", getImageUrlStr("https://img1.baidu.com/it/u=1966616150,2146512490&fm=253&fmt=auto&app=138&f=JPEG?w=751&h=500"));
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");
            configuration.setDirectoryForTemplateLoading(new File(Path));//ftl文件目录
            //输出文档路径及名称
            File outFile = new File(Path + "A.doc");
            Template template = configuration.getTemplate("A.ftl", "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    //图片地址
    public static String getImageUrlStr(String imgFile) {
    
    
        InputStream in = null;
        byte[] data = null;
        try {
    
    
            if (imgFile.contains("http://")||imgFile.contains("https://")){
    
    
                in = getInputStream(imgFile);
                data = new byte[in.available() * 100];
            }else {
    
    
                in = new FileInputStream(imgFile);
                data = new byte[in.available()];
            }
            in.read(data);
            in.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    public static InputStream getInputStream(String picUrl) {
    
    
        InputStream in = null;
        HttpURLConnection connection = null;
        try {
    
    
            URL url = new URL(picUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(10000);
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
    
    
                in = connection.getInputStream();
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return in;
    }

}

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45853881/article/details/129298494