POI Excel插入图片(网络路径、本地路径)

1 Maven依赖

        <!-- easyExcel  Excel文档处理工具     -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.8</version>
        </dependency>
        <!-- hutool工具包       -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.7</version>
        </dependency>

2 实现代码



    /**
     * 插入网络图片(单个单元格)
     *
     * @param workbook 文档对象
     * @param sheet    sheet页对象
     * @param url      网络路径
     * @param rowIndex 行号
     * @param colIndex 开始列号
     */
    public static void insertUrlImg(Workbook workbook, Sheet sheet, String url, int rowIndex, int colIndex) {
        insertUrlImg(workbook, sheet, url, rowIndex, rowIndex, colIndex, colIndex);
    }

    /**
     * 插入网络图片
     *
     * @param workbook      文档对象
     * @param sheet         sheet页对象
     * @param url           网络路径
     * @param beginRowIndex 开始行号
     * @param endRowIndex   结束行号
     * @param beginColIndex 开始列号
     * @param endColIndex   结束列号
     */
    public static void insertUrlImg(Workbook workbook, Sheet sheet, String url, int beginRowIndex, int endRowIndex
            , int beginColIndex, int endColIndex) {
        byte[] bytes = HttpUtil.downloadBytes(url);
        insertImg(workbook, sheet, bytes, beginRowIndex, endRowIndex, beginColIndex, endColIndex);
    }

    /**
     * 插入本地图片(单个单元格)
     *
     * @param workbook 文档对象
     * @param sheet    sheet页对象
     * @param url      本地路径
     * @param rowIndex 行号
     * @param colIndex 开始列号
     */
    public static void insertFileImg(Workbook workbook, Sheet sheet, String url, int rowIndex, int colIndex) throws Exception {
        insertFileImg(workbook, sheet, url, rowIndex, rowIndex, colIndex, colIndex);
    }

    /**
     * 插入本地图片
     *
     * @param workbook      文档对象
     * @param sheet         sheet页对象
     * @param url           本地路径
     * @param beginRowIndex 开始行号
     * @param endRowIndex   结束行号
     * @param beginColIndex 开始列号
     * @param endColIndex   结束列号
     */
    public static void insertFileImg(Workbook workbook, Sheet sheet, String url, int beginRowIndex, int endRowIndex
            , int beginColIndex, int endColIndex) throws Exception {
        //将本地路径的图片变成二进制流
        FileInputStream fileInputStream = new FileInputStream(new File(url));
        byte[] bytes = IoUtil.readBytes(fileInputStream);
        fileInputStream.close();
        //插入图片
        insertImg(workbook, sheet, bytes, beginRowIndex, endRowIndex, beginColIndex, endColIndex);
    }

    /**
     * 插入图片
     *
     * @param workbook      文档对象
     * @param sheet         sheet页对象
     * @param picture       图片二进制流数组
     * @param beginRowIndex 开始行号
     * @param endRowIndex   结束行号
     * @param beginColIndex 开始列号
     * @param endColIndex   结束列号
     */
    public static void insertImg(Workbook workbook, Sheet sheet, byte[] picture, int beginRowIndex, int endRowIndex
            , int beginColIndex, int endColIndex) {
        //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
        Drawing drawing = sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }
        //anchor主要用于设置图片的属性
        ClientAnchor anchor = null;
        if (workbook instanceof HSSFWorkbook) {
            anchor = new HSSFClientAnchor(0, 0, 255, 255, (short) beginColIndex, beginRowIndex, (short) (endColIndex + 1), endRowIndex + 1);
        } else if (workbook instanceof XSSFWorkbook) {
            anchor = new XSSFClientAnchor(0, 0, 255, 255, (short) beginColIndex, beginRowIndex, (short) (endColIndex + 1), endRowIndex + 1);
        }
        //插入图片
        drawing.createPicture(anchor, workbook.addPicture(picture, Workbook.PICTURE_TYPE_JPEG));
    }

3 调试代码

 /**
     * 测试插入网络图片
     */
    @Test
    public void testInsertUrlImg() {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            File file = new File("D:/easyexcel/testInsertUrlImg.xlsx");
            FileUtil.createNewFile(file);
            String imgUrl = "https://profile.csdnimg.cn/9/5/B/1_qq_38974638";
            //插入图片
            POIExcelUtil.insertUrlImg(workbook, sheet, imgUrl, 0, 3, 0, 1);
            workbook.write(new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试插入本地图片
     */
    @Test
    public void testInsertFileImg() {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            File file = new File("D:/easyexcel/testInsertFileImg.xlsx");
            FileUtil.createNewFile(file);
            String imgUrl = "C:\\Users\\22185\\Desktop\\875dc98183344af3843c7ac619154c6f_qq_38974638.jpg";
            //插入图片
            POIExcelUtil.insertFileImg(workbook, sheet, imgUrl, 0, 3, 0, 1);
            workbook.write(new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4 调试结果

 注:

(1)支持图片地址为网络路径或网络路径,暂时只支持jpg格式图片其他格式图片未测试。

(2)源码请查看Gitee。

xudongbase: 主要是项目中可以用到的共通方法https://gitee.com/xudong_master/xudongbase

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值