在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。

  一、获取HSSFPatriarch对象,

  二、new HSSFClientAnchor对象,

  三、调用createPicture方法即可。

  实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:

public class ExcelImageTest {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut); HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("test picture");
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("D:/测试Excel.xls");
// 写入excel文件
wb.write(fileOut);
System.out.println("----Excle文件已生成------");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

如下为执行后的结果:

至于为什么会是这样的结果,主要是因为HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)这个构造函数造成的,下面我就来解释这个构造函数:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各个参数的含义如下:

dx1:the x coordinate within the first cell。

dy1:the y coordinate within the first cell。

dx2:the x coordinate within the second cell。

dy2:the y coordinate within the second cell。

col1:the column (0 based) of the first cell。

row1:the row (0 based) of the first cell。

col2:the column (0 based) of the second cell。

row2:the row (0 based) of the second cell。

这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。

下面是有两个不同的构造函数所创建的,从这幅图中我们可以清晰看到上面八个参数的含义和不同之处。

上面是插入一张图片,那么实现插入多张图片呢?其实很简单,构造多个不同的HSSFClientAnchor对象,控制好那八个参数,如下:

HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8);
HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16); //插入图片
patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

其余代码一样,得到如下结果:

转载于:http://blog.csdn.net/chenssy/article/details/20524563

Java Excel 插入图片的更多相关文章

  1. Excel催化剂开源第40波-Excel插入图片做到极致的效果

    不知道是开发人员的自我要求不高还是用户的使用宽容度足够大,在众多Excel插入图片的版本中,都没有考虑到许多的可大幅度提升用户体验的细节处理. Excel催化剂虽然开发水平有限,但也在有限的能力下,尽 ...

  2. NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片

    --------------------- 本文来自 天水宇 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xxs77ch/article/details/50553 ...

  3. python 向excel 插入图片

    这是工作中一个真实的需求. 要做gt excel 表,表中要插入图片. 1.要把图片resize 基本相同的大小. 2.通过一下脚本插入图片到excel #!/usr/bin/env python3 ...

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

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

  5. 2.4.5 用NPOI操作EXCEL--插入图片

    我们知道,在Excel中是可以插入图片的.操作菜单是“插入->图片”,然后选择要插入图片,可以很容易地在Excel插入图片.同样,在NPOI中,利用代码也可以实现同样的效果.在NPOI中插入图片 ...

  6. Java操作Excel(读、写、搜索关键字、插入图片)

    import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...

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

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

  8. java 导出blob图片到excel

    实现功能,导出当前页面显示员工的图片,核心代码已给出,仅供参考, 如需转载请注明出处http://www.cnblogs.com/wangjianguang/p/7852060.html 随便再扯2句 ...

  9. java poi 向excel写入图片

    import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; impo ...

随机推荐

  1. 流程控制--for序列

    In []: list1 = [,,,] In []: for i in list1: ...: print i ...: In []: for i in list1: print i, ...: / ...

  2. Hierarchical Attention Based Semi-supervised Network Representation Learning

    Hierarchical Attention Based Semi-supervised Network Representation Learning 1. 任务 给定:节点信息网络 目标:为每个节 ...

  3. 设置Eclipse/MyEclipse中编辑界面点击任何文件后Package Explorer导航自动定位该文件

    原文:http://www.myexception.cn/eclipse/425836.html 设置步骤: 导航Package Explorer的右上角有一个黄色双向箭头图标,鼠标移动到上面提示“L ...

  4. mysql远程访问cannot connect(10038) 问题解决的过程

    今天用Navicat访问虚拟机上的mysql,无法访问报cannot connect(10038). 首先看是否可以telnet,本机cmd,telnet 192.168.209.128 3306,结 ...

  5. linux命令(31):lsof命令

    1.递归查看某个目录的文件信息: lsof  test/test1 2.不使用+D选项,遍历查看某个目录的所有文件信息的方法 :lsof |grep 'test/test3' 3.列出某个用户打开的文 ...

  6. LeetCode214. Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. [已解决]#1142 - SELECT command denied to user ''@'localhost' for table 'pma_table_uiprefs'

    症状:在phpmyadmin那边打不开表,提示 #1142 - SELECT command denied to user ''@'localhost' for table 'pma_table_ui ...

  8. python No migrations to apply

    错误显示:  “No migrations to apply” 错误情况:python在通过model同步数据库时,提示 No migrations to apply 查看数据库,新表没有被创建,只是 ...

  9. WeGame导致WSL无法监听端口问题

    Windows 10 系统自带Linux子系统(WSL),可以方便的使用WSL运行Linux程序和脚本.笔者在WSL上运行Redis时突然发现无法监听6379端口,尝试重新安装WSL无果. 后来重新安 ...

  10. WPS设置去广告

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha WPS设置去广告 设置密码和权限