如何使用word模板生成word文档(文本,图片)

注意:只针对数据信息与图片信息进行生成。


 一,准备工作

1,编辑word模板,变量信息以${变量名称}表示,图片要使用临时图片占用位置。

 

2,转换格式word输出为Word.xml文档格式,在手动改为ftl格式。

4,检查所有的变量${xxxx}在转换格式时是否错位。

二,代码

 

package com.maxinyang.word;

import com.itextpdf.text.pdf.BaseFont;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.apache.tools.ant.types.resources.AbstractClasspathResource;
import sun.misc.BASE64Encoder;

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

/**
 * 作者:killgod
 * 口号:天王盖地虎
 */
public class WriteWord {
    /**
     * word生成需要org.freemarker.freemarker类
     * 主要介绍如何使用模板生成(包含图片)
     * 主要是用的类:
     * 1.BABSE64Encoder类:因为转为xml文件的图片编码使用的是BASE64
     * 2.Configuration类: 配置类使用它获取到模板类对象
     * 3.Template类:使用他来完成参数与${xxxx}映射
     * 
     * 注意:
     *  1.map中key必须与word文档中${xxx}一致才能完成赋值
     *  2.word生成图片必须完成前提准备
     *
     * @param args
     */
    public static void main(String[] args) {
        
        Map<String, Object> map = new HashMap<>();
        // 将word所需要的数据放入map集合
        map.put("name", "张三");
        map.put("password", "laoma123");
        map.put("age", "18");
        // 读取图片将其转为base64格式
        String imgPaht = downPicture("F:\\学习测试文件\\PDF\\2.png");
        map.put("img", imgPaht);
        // 核心方法
        generateWord(map);


    }

    /**
     * 将图片转为base64格式返回处理好的字符串
     *
     * @param picture
     */
    public static String downPicture(String picture) {
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(picture);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    /**
     * 生成word文档核心方法
     * @param params
     */
    public static void generateWord(Map<String, Object> params) {
        // 读取模板文件
        Version incompatibleImprovements;
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        // 模板文件.ftl文件位置 注意:目录是指文件存在的文件夹。
        File templateContents = new File("F:\\学习测试文件\\wrod\\");
        String templateFileName = "测试word模板.ftl";
        BufferedWriter bufferedWriter = null;
        try {
            // 通过配置类读取模板获取模板类
            configuration.setDirectoryForTemplateLoading(templateContents);
            Template template = configuration.getTemplate(templateFileName, "UTF-8");

            // 输出文件名称
            long timeMillis = System.currentTimeMillis();
            String outFileName = "xxcs_" + timeMillis + ".doc";
            String outFile = "F:\\学习测试文件\\word\\result\\";
            //获取输出文件路径
            File outFileContents = new File(outFile);
            // 判断文件路径是否存在
            if (!outFileContents.exists()) {
                outFileContents.mkdirs();
            }
            outFileContents = new File(outFile + outFileName);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileContents), "UTF-8"));

            // 参数与输出流
            template.process(params, bufferedWriter);

        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }finally {
            if(bufferedWriter!=null){
                try {
                    //关闭流
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值