poi往word单元格插入图片,支持本地图片和网络图片

简介: 最近word导出时需要导出图片,在网上翻了会就发现了好心人分享的代码,但是只支持本地文件读取,我们项目需要从其他服务器获取图片,那肯定得使用网络方式了,于是在源代码上进行了完善,可接收本地和网络两种url方式。

poi版本:4.1.2


最近word导出时需要导出图片,在网上翻了会就发现了好心人分享的代码,但是只支持本地文件读取,我们项目需要从其他服务器获取图片,那肯定得使用网络方式了,于是在源代码上进行了完善,可接收本地和网络两种url方式。


完善后的代码如下:

  /**
     * 批量插入多张图片,一张一行
     * @param cell word表格的某单元格
     * @param urlList 支持本地文件和网络文件
     * @throws Exception
     */
    public void setCellImg(XWPFTableCell cell, List<String> urlList) {
        try{
            //获取单元格的段落
            XWPFParagraph paragraphs = cell.getParagraphs().get(0);
            XWPFRun run = paragraphs.getRuns().isEmpty() ? paragraphs.createRun() : paragraphs.getRuns().get(0);
            int index=0;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            InputStream is;
            File image;
            for(String path : urlList){
                //判断图片的格式
                int format=0;
                if (path.endsWith(".emf")) {
                    format = XWPFDocument.PICTURE_TYPE_EMF;
                } else if (path.endsWith(".wmf")) {
                    format = XWPFDocument.PICTURE_TYPE_WMF;
                } else if (path.endsWith(".pict")) {
                    format = XWPFDocument.PICTURE_TYPE_PICT;
                } else if (path.endsWith(".jpeg") || path.endsWith(".jpg")) {
                    format = XWPFDocument.PICTURE_TYPE_JPEG;
                } else if (path.endsWith(".png")) {
                    format = XWPFDocument.PICTURE_TYPE_PNG;
                } else if (path.endsWith(".dib")) {
                    format = XWPFDocument.PICTURE_TYPE_DIB;
                } else if (path.endsWith(".gif")) {
                    format = XWPFDocument.PICTURE_TYPE_GIF;
                } else if (path.endsWith(".tiff")) {
                    format = XWPFDocument.PICTURE_TYPE_TIFF;
                } else if (path.endsWith(".eps")) {
                    format = XWPFDocument.PICTURE_TYPE_EPS;
                } else if (path.endsWith(".bmp")) {
                    format = XWPFDocument.PICTURE_TYPE_BMP;
                } else if (path.endsWith(".wpg")) {
                    format = XWPFDocument.PICTURE_TYPE_WPG;
                } else {
                    logger.error("不支持的图片类型Unsupported picture: " + path +
                            ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
                    continue;
                }
                //计算适合文档宽高的图片EMU数值
                BufferedImage read;
                if(path.startsWith("http")){
                    output = getByteArrayOutputStreamByUrl(path);
                    is = new ByteArrayInputStream(output.toByteArray());
                    read = ImageIO.read(is);
                    // is被读取后需要重新写入
                    is = new ByteArrayInputStream(output.toByteArray());
                }else {
                    image = new File(path);
                    //判断图片是否存在
                    if(!image.exists()){
                        continue;
                    }
                    read = ImageIO.read(image);
                    //获取图片文件流
                    is = new FileInputStream(path);
                }
                int width = Units.toEMU(read.getWidth());
                int height = Units.toEMU(read.getHeight());
                //1 EMU = 1/914400英寸= 1/36000 mm,15是word文档中图片能设置的最大宽度cm
                double cm=5; // 图片宽度 单位cm
                if(width/360000>cm){
                    NumberFormat f = NumberFormat.getNumberInstance();
                    f.setMaximumFractionDigits(0);
                    f.setRoundingMode(RoundingMode.UP);
                    Double d=width/360000/cm;
                    width = Integer.valueOf(f.format(width/d).replace(",",""));
                    height = Integer.valueOf(f.format(height/d).replace(",",""));
                }
                run.addPicture(is, format, path, width, height);
                is.close();
                output.close();
                if(index!=urlList.size()-1){
                    run.addBreak();//换行
                }
                index++;
            }
        }catch (Exception e){
            logger.error("图片写入到word失败!",e);
        }
    }
    /**
     * 将输入流保存到字节数组中,可用于后面实现多次读取该输入流的数据
     * @param httpUrl
     * @return
     */
    public ByteArrayOutputStream getByteArrayOutputStreamByUrl(String httpUrl) {
        HttpURLConnection conn = null;
        try {
            URL url = new URL(httpUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(20 * 1000);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(), output);
            return output;
        } catch (Exception e) {
            logger.error("getByteArrayOutputStreamByUrl 异常,exception is {}", e);
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception e) {
            }
        }
        return null;
    }


导出效果图:

74847627ef524f81bd8f9af0ac8944b2.png

原文链接:https://blog.csdn.net/weixin_42949219/article/details/124445069

目录
相关文章
|
20天前
|
机器学习/深度学习 算法 计算机视觉
卷积神经网络中的卷积层,如何提取图片的特征?
卷积神经网络中的卷积层,如何提取图片的特征?
29 0
|
1月前
|
SQL 机器学习/深度学习 分布式计算
Spark【基础知识 01】【简介】(部分图片来源于网络)
【2月更文挑战第12天】Spark【基础知识 01】【简介】(部分图片来源于网络)
27 2
|
1月前
|
消息中间件 存储 缓存
Kafka【基础知识 01】消息队列介绍+Kafka架构及核心概念(图片来源于网络)
【2月更文挑战第20天】Kafka【基础知识 01】消息队列介绍+Kafka架构及核心概念(图片来源于网络)
94 2
|
6月前
|
数据采集 数据安全/隐私保护
Haskell网络编程:从数据采集到图片分析
爬虫技术在当今信息时代中发挥着关键作用,用于从互联网上获取数据并进行分析。本文将介绍如何使用Haskell进行网络编程,从数据采集到图片分析,为你提供一个清晰的指南。
Haskell网络编程:从数据采集到图片分析
|
14天前
|
分布式计算 资源调度 Hadoop
Hadoop【基础知识 03+04】【Hadoop集群资源管理器yarn】(图片来源于网络)(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
【4月更文挑战第5天】Hadoop【基础知识 03】【Hadoop集群资源管理器yarn】(图片来源于网络)Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
41 9
|
15天前
|
分布式计算 资源调度 Hadoop
Hadoop【基础知识 03】【Hadoop集群资源管理器yarn】(图片来源于网络)
【4月更文挑战第4天】Hadoop【基础知识 03】【Hadoop集群资源管理器yarn】(图片来源于网络)
23 4
|
1月前
|
消息中间件 存储 缓存
Kafka【基础知识 02】集群+副本机制+数据请求+物理存储+数据存储设计(图片来源于网络)
【2月更文挑战第20天】Kafka【基础知识 02】集群+副本机制+数据请求+物理存储+数据存储设计(图片来源于网络)
43 1
|
1月前
|
分布式计算 Hadoop Java
Spark【基础知识 03】【RDD常用算子详解】(图片来源于网络)
【2月更文挑战第14天】Spark【基础知识 03】【RDD常用算子详解】(图片来源于网络)
57 1
|
1月前
|
存储 缓存 分布式计算
Spark【基础知识 02】【弹性式数据集RDDs】(部分图片来源于网络)
【2月更文挑战第13天】Spark【基础知识 02】【弹性式数据集RDDs】(部分图片来源于网络)
34 1
|
1月前
|
数据采集 Web App开发 数据处理
Ruby网络爬虫教程:从入门到精通下载图片
Ruby网络爬虫教程:从入门到精通下载图片