POI向Excel的单元格中写入图片

时光总嘲笑我的痴心妄想 提交于 2020-04-30 19:55:41

HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 
XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 

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

针对.xls文件导出图片

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("E:/测试图片.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);   
            //注意:这个方法在新版本的POI中参数类型改成了(AnchorType anchorType) 
       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(); } } } } }

本项目导入的POI版本是poi-3.11-20141221.jar,setAnchorType(int anchorType)

 工具方法

  //插入某个图片到指定索引的位置  
    private static void insertImage(HSSFWorkbook wb,HSSFPatriarch pa,byte[] data,int row,int column,int index){  
        //单元格为标,以左上为起点,向右移,范围0-1023  dx1 must be between 0 and 1023  
        int x1=0;  
        //单元格为标,以左上为起点,向下移,范围0-1023  dy1 must be between 0 and 255  
        int y1=0;  
        //单元格为标,以右上为起点,向右移,范围0-1023  dx1 must be between 0 and 1023  
        int x2=100;  
        //单元格为标,以右下为起点,向下移,范围0-1023  dy1 must be between 0 and 255  
        int y2=22;
     //后面四个参数表示图片左上角和右下角的坐标
        //col1 图片的左上角放在第几个列cell,
     // row1 图片的左上角放在第几个行cell, 

          // col2 图片的右下角放在第几个列cell, 
          // row2 图片的右下角放在第几个行cell,

        HSSFClientAnchor anchor = new HSSFClientAnchor(x1,y1,x2,y2,(short)column,row,(short)column,row);  
          
        anchor.setAnchorType(3);  
        pa.createPicture(anchor , wb.addPicture(data,HSSFWorkbook.PICTURE_TYPE_JPEG));  
    }  

针对.xlsx导出Excel图片

package com.org.apache.poi.xssf;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class StartPoiExcelWriterImg {
    public static void main(String[] args) {  
        FileOutputStream fileOut = null;     
        BufferedImage bufferImg = null;     
       //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray    
       try {  
           ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();     
           bufferImg = ImageIO.read(new File("D:/测试图片.jpg"));     
           ImageIO.write(bufferImg, "jpg", byteArrayOut);  
             
           XSSFWorkbook wb = new XSSFWorkbook();     
           XSSFSheet sheet1 = wb.createSheet("Sheet1");    
           //XSSFSheet sheet1 = wb.getSheet("Sheet1");
           //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)  
           XSSFDrawing patriarch = sheet1.createDrawingPatriarch();     
           //anchor主要用于设置图片的属性  
           XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);     
           anchor.setAnchorType(3);     
           //插入图片    
           patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));   
           fileOut = new FileOutputStream("C:\\Users\\huage\\Desktop\\121231\\11111.xlsx");     
           // 写入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();  
               }  
           }  
       }  
   }  
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!