资讯专栏INFORMATION COLUMN

java poi 在excel中插入图片

robin / 1468人阅读

摘要:中导出数据是常见的功能,最近遇到一个需求是在中插入图片。处理及其他微软办公系列软件常用的就是,它也是支持图片插入的。

java web中导出excel数据是常见的功能,最近遇到一个需求是在excel中插入图片。处理excel及其他微软办公系列软件常用的就是apache poi,它也是支持图片插入的。插入图片最主要的用到HSSFClientAnchor,文档介绍如下:
public HSSFClientAnchor(int dx1,

            int dy1,
            int dx2,
            int dy2,
            short col1,
            int row1,
            short col2,
            int row2)

Creates a new client anchor and sets the top-left and bottom-right coordinates of the anchor. Note: Microsoft Excel seems to sometimes disallow higher y1 than y2 or higher x1 than x2, you might need to reverse them and draw shapes vertically or horizontally flipped!

简单的demo:

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelExport {

    public static void main(String[] args) {
         FileOutputStream fileOut = null;   
         BufferedImage bufferImg = null;   
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          //加载图片
            bufferImg = ImageIO.read(new File("e:/1.jpg"));   
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            HSSFWorkbook wb = new HSSFWorkbook();   
            HSSFSheet sheet1 = wb.createSheet("sheet1");  
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
            /**
                 dx1 - the x coordinate within the first cell.//定义了图片在第一个cell内的偏移x坐标,既左上角所在cell的偏移x坐标,一般可设0
                dy1 - the y coordinate within the first cell.//定义了图片在第一个cell的偏移y坐标,既左上角所在cell的偏移y坐标,一般可设0
                dx2 - the x coordinate within the second cell.//定义了图片在第二个cell的偏移x坐标,既右下角所在cell的偏移x坐标,一般可设0
                dy2 - the y coordinate within the second cell.//定义了图片在第二个cell的偏移y坐标,既右下角所在cell的偏移y坐标,一般可设0
                col1 - the column (0 based) of the first cell.//第一个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.//图片右下角所在行
             */
            HSSFClientAnchor anchor = new HSSFClientAnchor(-100, 0, 0, 0,(short) 2, 2, (short) 5, 8);   
            //插入图片  
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
            fileOut = new FileOutputStream("e:/excel.xls");   
            // 输出文件 
             wb.write(fileOut);   
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

关于dx1的设置的说明,dx2,dy1等都是类似的

关于一个excel设置多张图片的demo

package com.poi;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelExport {

    public static void main(String[] args) {
         FileOutputStream fileOut = null;   
         BufferedImage bufferImg = null;   
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          //加载图片
            bufferImg = ImageIO.read(new File("e:/1.jpg"));   
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            HSSFWorkbook wb = new HSSFWorkbook();   
            HSSFSheet sheet1 = wb.createSheet("sheet1");  
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,(short) 2, 2, (short) 5, 8);
            //插入图片 1 
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            
            //图片2
            anchor = new HSSFClientAnchor(200, 0, 0, 0,(short) 2, 9, (short) 5, 15);
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            fileOut = new FileOutputStream("e:/excel.xls");   
            // 输出文件 
             wb.write(fileOut);   
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

总体来说使用poi还是很方便的。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66392.html

相关文章

  • POI如何高效导出百万级Excel数据?

    摘要:阅读原文如何高效导出百万级数据在一个具有统计功能的系统中,导出功能几乎是一定的,如何导出导出的数据有多少如何高效的导出简介什么是就不用介绍了,这里主要说明不同版本下每个下的行列限制。 阅读原文:POI如何高效导出百万级Excel数据? 在一个具有统计功能的系统中,导出excel功能几乎是一定的,如何导出excel?导出的数据有多少?如何高效的导出? Excel简介什么是excel就不用...

    lemanli 评论0 收藏0
  • 慕课网_《解密JAVA实现Excel导入导出》学习总结

    时间:2017年07月06日星期四说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:无学习源码:https://github.com/zccodere/s... 第一章:课程介绍 1-1 预备知识 基础知识 struts2框架(上传下载功能) xml解析技术(导入模板) JQuery EasyUI(前台美观) 课程目录 实现方式 定制导入模版 导入文件 导...

    enrecul101 评论0 收藏0
  • 基于POIExcel工具类

    摘要:一般订单打印纸高度毫米字母或半角字符判断是否为全角全角符号及中文连续出现,认为是单词组成部分的字符和间单页的票据右端联页宽占用的宽度回退一行每减一次少一行实际高度的距离补足行高拉高的行内容要调整使用靠上对齐补充 import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.Region; import ...

    sevi_stuo 评论0 收藏0
  • 使用ApachePOI生成XLSX格式Excel文档大数据量导出

    摘要:最近在做使用进行大数据量导出,现在把其整理成工具类供大家参考。版本增加了前缀为相关的类,主要用于大数据量的写入与读取。 最近在做使用POI进行大数据量导出,现在把其整理成工具类供大家参考。Apache POI 3.8版本增加了前缀为SXSSF相关的类,主要用于大数据量的写入与读取。关于ApachePOI导出Excel基本的使用我这里就不详解了,具体参考: Apache POI官方网站...

    Shihira 评论0 收藏0
  • Java导出excel文件

    摘要:效果预览导出文件效果点击下载弹出框效果代码总览为公司业务代码,大多为从缓存或者数据库中获取导出数据,不影响导出功能。导出导出导出所有在线离线用户成功导出所有在线离线用户失败引用导出表格 需求 将每个xmpp机房的在线/离线用户信息导出到Excel表格中(定时任务+网页按钮),并在网页上提供下载按钮进行下载。 效果预览 showImg(https://segmentfault.com/i...

    import. 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<