向word文档中插入文字和图片

package com.xxxx;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import org.springframework.core.io.ClassPathResource;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Text {
    public static void  asd() {
        try {
            //待处理的图片
            String imgFile = "D:/123.jpg";
            //将图片转化成base64
            InputStream in = null;
            byte[] data1;
            //读取图片字节数组
                in = new FileInputStream(imgFile);
                data1 = new byte[in.available()];
                in.read(data1);
                in.close();
            //获取base64码转换成byte[]数组
            byte[] bytes = new byte[0];
            //对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            //base64编码会自动换行
            String imageData = encoder.encode(data1).replaceAll("[\\s*\t\n\r]", "");
            //将base64转化为流
            bytes = new BASE64Decoder().decodeBuffer(imageData.trim());
            //转化为输入流
            File file = new File("");
            String filePath1 = file.getCanonicalPath();//获取项目路径
            System.out.println(filePath1+"\\p.jpg");
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);//输入流输入bytes数组
            int leng;
            byte[] b = new byte[1024];
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File("filePath1.jpg"));//打开输入流生成图片
            while ((leng = inputStream.read(b)) != -1) {
                //将byte写入硬盘
                imageOutput.write(b, 0, leng);
            }
            imageOutput.close();
            Map<String, Object> data = new HashMap<String, Object>();
            // 文本
            data.put("name", "张三");
            //图片(自定义设置高/宽)
            data.put("photo", new PictureRenderData(127, 185, "filePath1.jpg"));
            // 写入word输出
            System.out.println(filePath1+"\\wordTest.docx");
            //获取模板
            ClassPathResource template = new ClassPathResource("D:/360/img.docx");
            //读取图片路径
            String filePath = template.getPath();
            //填充word
            XWPFTemplate xwpfTemplate = XWPFTemplate.compile(filePath).render(data);
            //生成word路径
            String docName = filePath1+System.currentTimeMillis() + ".docx";
            //新建File文件对象
            File targetFile = new File(docName);
            //FileOutputStream文件字节输出流
            FileOutputStream out = new FileOutputStream(targetFile);     
            xwpfTemplate.write(out);
            out.flush();          //刷新
            out.close();         //关流
            xwpfTemplate.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值