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. 【hdu 4135】Co-prime

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1 ...

  2. Java数据结构(排序篇)

    冒泡排序:是经过n-1趟子排序完毕的,第i趟子排序从第1个数至第n-i个数,若第i个数比后一个数大(则升序,小则降序)则交换两数.大泡在上,小泡在下. 选择排序:每一趟从待排序的数据元素中选出最小(或 ...

  3. [Tailwind] Get started with Tailwindcss

    In this lesson, we learn how to generate CSS utility classes from Tailwind's JavaScript config file. ...

  4. Android 经常使用设计模式(一)

    由于项目变更的频繁性,作为一名程序猿,我们须要掌握设计模式的必要性.就不言而喻~~.以下就是一些我自己学习的设计模式总结. 接下来,主要是针对几个比較经常使用模式进行解说,主要是以下几种: 观察者模式 ...

  5. robot framework环境配置

    1.Robot framework的安装 作用:web自动化测试框架. RF框架是基于python 的,所以一定要有python环境.网上可以自行查找. 下载地址:https://pypi.pytho ...

  6. Android - 使用JD-GUI反编译Android代码

    使用JD-GUI反编译Android代码 本文地址: http://blog.csdn.net/caroline_wendy Android程序出现Bug时,须要依据Bug寻找问题出错的地方; 须要使 ...

  7. Redis命令-HyperLogLog

    HyperLogLog数据结构简单介绍 能够看http://www.cnblogs.com/ysuzhaixuefei/p/4052110.html  博客,介绍的相对照较清晰. HyperLogLo ...

  8. 2014.04.17,转帖,关于FFT的结果为什么要除以N

    http://www.chinavib.com/forum/viewthread.php?tid=23665&highlight= 关于这个问题,我看到的书好像都没有进行解释,这里我试着解释下 ...

  9. SqlServer Function 实例

    ① sql server function 创建 这里使用一个计算年龄精确到分的function作为一个demo, create Function [dbo].[fn_GetAge] ( @BIRTH ...

  10. Js正则表达式数字或者带小数点的数字

    function chk() { var patrn = /^\d+(\.\d+)?$/; var result = true; $("input[type=text]").eac ...