POI的操作Excel时,不可避免有操作图片的处理。怎么插入图片呢?网上也有不少介绍。

下面的代码是向Excel中插入多张图片的例子:

public static void main(String[] args) {    
        FileOutputStream fileOut = null;    
        BufferedImage bufferImg = null;    
        BufferedImage bufferImg1 = null;    
        try {    
            // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray    
            // 读入图片1    
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();    
            bufferImg = ImageIO.read(new File("d:\\test11.jpg"));    
            ImageIO.write(bufferImg, "jpg", byteArrayOut);    
                
            // 读入图片2    
            ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();    
            bufferImg1 = ImageIO.read(new File("d:\\test22.png"));    
            ImageIO.write(bufferImg1, "png", byteArrayOut1);    
   
            // 创建一个工作薄    
            HSSFWorkbook wb = new HSSFWorkbook();    
            HSSFSheet sheet1 = wb.createSheet("test picture");    
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();    
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,    
                    (short) 1, 1, (short) 5, 5);    
            anchor.setAnchorType(3);    
            HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 255, 255,    
                    (short) 6, 6, (short) 10, 10);    
            anchor1.setAnchorType(3);    
            // 插入图片1    
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut    
                    .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));    
            // 插入图片2    
            patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1    
                    .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));    
   
            fileOut = new FileOutputStream("d:/workbook.xls");    
            // 写入excel文件    
            wb.write(fileOut);    
            fileOut.close();    
        } catch (IOException io) {    
            io.printStackTrace();    
            System.out.println("erorr : " + io.getMessage());    
        } finally {    
            if (fileOut != null) {    
                try {    
                    fileOut.close();    
                } catch (IOException e) {    
                    e.printStackTrace();    
                }    
            }    
        }    
    }

这样执行后的效果如下:(完全按照HSSFClientAnchor设置的参数显示)

这边的效果没有保持原来的倍率,有点失真了,是因为HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 5, 5); 这个构造函数的原因。

HSSFClientAnchor构造函数参数的意义如下:

* @param dx1   the x coordinate within the first cell.
     * @param dy1   the y coordinate within the first cell.
     * @param dx2   the x coordinate within the second cell.
     * @param dy2   the y coordinate within the second cell.
     * @param col1  the column (0 based) of the first cell.
     * @param row1  the row (0 based) of the first cell.
     * @param col2  the column (0 based) of the second cell.
     * @param row2  the row (0 based) of the second cell.

其中dx1,dy1这个点是定义图片在开始cell中的起始位置。这个点是开始cell的左上为原点,相对比率来确定的。不是绝对坐标。

dx2,dy2这个点是定义图片在终了cell的终了位置。这个点是终了cell的左上为原点,相对比率来确定的。不是绝对坐标。

col1,row1是定义开始cell。

col2,row2是定义终了cell。

如果我们想要保持原始的图片大小,改一下上面代码中的下面部分就可以了。

修改前:patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

修改后:patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
                    .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)).resize(1);

修改后效果如下:

其中resize是放大或缩小的函数。用了这个函数后,HSSFClientAnchor构造函数中的图片显示的终了cell位置就不起作用了。

原文:http://blog.163.com/alpsdyk2001@126/blog/static/5279414820099266640415/

POI实现Excel2003插入多张图片的更多相关文章

  1. 完美解决 向UILable 文字最后插入N张图片,支持向限制行数的UILable 最后一行插入,多余文字显示...

    效果: ====直接上代码吧=== // // UILabel+StringFrame.h // QYER // // Created by qyer on 15/3/19. // Copyright ...

  2. 如何在一张ppt中插入多张图片并能依次播放

    我们在做ppt的过程中,有时遇到在一张ppt中插入多张图片还想让其能依次播放的情况,针对上述情况我们可以根据下列步骤进行设置.(新手必看) 1.首先,用鼠标点击桌面Microsoft  PowerPo ...

  3. php 连接oracle插入多张图片的方法

    php连接oracle数据库的时候,其查询.更新.删除数据和MySQL类似,但是增加数据.特别是图片的时候就很不一样,这里面涉及到要创建一个blob对象,用blod对象去保存php图片,下面是当插入多 ...

  4. POI导出Word插入复选框

    POI功能比较强大,但是有些不常用功能比如插入特殊符号,不知道API怎么调用 Word里要插入复选框,首先想到的是POI有没有提供现成的API,搜了一番,貌似都说不直接支持 http://stacko ...

  5. 解决java poi导出excel2003不能超过65536行的问题

    java poi在导出数据到excel2003工作表中时一个工作表只能存储65536行数据,如果超过这个数据就会失败,excel2007并没有这个问题,但是为了兼容性我们通常都是导出到2003版本上的 ...

  6. 移动端 canvas插入多张图片生成一张可保存到手机图片

    第一次写随笔,想把开发中遇到的问题与大家分享,可能会让您少走一步弯路. 先看下效果图: 代码分三部分为大家展示: 1.html 部分 <div id="myQrcontainer&qu ...

  7. element-ui里面的table插入多张图片

    <el-form-item label="身份证正反面" label-width="120px"> <section v-if="t ...

  8. java POI实现向Excel中插入图片

          做Web开发免不了要与Excel打交道.今天老大给我一个任务-导出Excel.开始想的还是蛮简单的,无非就是查找,构建Excel,response下载即可.但是有一点不同,就是要加入图片, ...

  9. java 在Excel中插入图片 POI实现

    一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...

随机推荐

  1. UVA11827 Maximum GCD

    /* UVA11827 Maximum GCD https://vjudge.net/contest/153365#problem/V 数论 gcd 水题,然而读入比较坑 * */ #include ...

  2. HDU5976 Detachment

    /* HDU5976 Detachment http://acm.hdu.edu.cn/showproblem.php?pid=5976 数论 等差数列 * * */ #include <cst ...

  3. daning links 系列

    1001 Easy Finding POJ-3740 1002 Power Stations HDOJ-3663 1003 Treasure Map ZOJ-3209 1004 Lamp HDOJ-2 ...

  4. ie版本

    <!--[if lte IE 6]> 自定义代码 <![endif]-->

  5. BA--冷源系统原理图解

  6. hdu 4717 The Moving Points(三分)

    http://acm.hdu.edu.cn/showproblem.php?pid=4717 大致题意:给出每一个点的坐标以及每一个点移动的速度和方向. 问在那一时刻点集中最远的距离在全部时刻的最远距 ...

  7. [Tailwind] Apply mobile-first Responsive Classes in Tailwind

    In this lesson, we take a look at tailwind's mobile-first CSS architecture and learn how to apply st ...

  8. intel dpdk在ubuntu12.04中測试testpmd、helloworld程序

    一.測试环境 操作系统:ubuntu12.04   x86_64 dpdk版本号:1.6.0r2 虚拟机:vmware 10 网卡: Intel Corporation 82545EM Gigabit ...

  9. gitlab一键安装 笔记

    0 简单介绍bitnami和gitlab bitnami BitNami是一个开源项目,该项目产生的开源软件包安装 Web应用程序和解决方式堆栈.以及虚拟设备. bitnami主办Bitrock公司成 ...

  10. codeforces Towers 题解

    Little Vasya has received a young builder's kit. The kit consists of several wooden bars, the length ...