Java操作POI4.1.0向Excel添加图片兼容03(.xls)及07后(xlsx)两个版本

1.说明

此java程序是操作poi向excel添加图片,两个函数,分别是03的版本和07的版本,poi版本用的是4.1.0,实测好用。

2.引入pom

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>

3.实体类,我自己定义的,定位图片在sheet的位置。

package com.stydy.test;
public class PicturePosition {

	private int dx1;//the x coordinate within the first cell 第一个单元格内的x坐标
	private int dy1;//the y coordinate within the first cell 第一个单元格内的y坐标
	private int dx2;//the x coordinate within the second cell 第二个单元格中的x坐标
	private int dy2;//the y coordinate within the second cell 第二个单元格中的y坐标
	private short col1;//the column (0 based) of the first cell 第一个单元格的列(基于0)
	private int row1;//the row (0 based) of the first cell 第一个单元格的行(基于0)
	private short col2;//the column (0 based) of the second cell 第二个单元格的列(基于0)
	private int row2;//the row (0 based) of the second cell 第二个单元格的行(基于0)
	
	public PicturePosition() {
		super();
	}
	public PicturePosition(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) {
		super();
		this.dx1 = dx1;
		this.dy1 = dy1;
		this.dx2 = dx2;
		this.dy2 = dy2;
		this.col1 = col1;
		this.row1 = row1;
		this.col2 = col2;
		this.row2 = row2;
	}
	public int getDx1() {
		return dx1;
	}
	public void setDx1(int dx1) {
		this.dx1 = dx1;
	}
	public int getDy1() {
		return dy1;
	}
	public void setDy1(int dy1) {
		this.dy1 = dy1;
	}
	public int getDx2() {
		return dx2;
	}
	public void setDx2(int dx2) {
		this.dx2 = dx2;
	}
	public int getDy2() {
		return dy2;
	}
	public void setDy2(int dy2) {
		this.dy2 = dy2;
	}
	public short getCol1() {
		return col1;
	}
	public void setCol1(short col1) {
		this.col1 = col1;
	}
	public int getRow1() {
		return row1;
	}
	public void setRow1(int row1) {
		this.row1 = row1;
	}
	public short getCol2() {
		return col2;
	}
	public void setCol2(short col2) {
		this.col2 = col2;
	}
	public int getRow2() {
		return row2;
	}
	public void setRow2(int row2) {
		this.row2 = row2;
	}
	
	
}

4.具体插入图片函数

这个是07版

public static void AddPictureToExcel(XSSFWorkbook workbook,ByteArrayOutputStream byteArrayOutputStream,PicturePosition picturePosition) {
		int sheetNumber=workbook.getNumberOfSheets();
		for(int i=0;i<sheetNumber;i++) {
			XSSFSheet sheet=workbook.getSheetAt(i);
			XSSFDrawing drawing=sheet.createDrawingPatriarch();
			XSSFClientAnchor anchor=new XSSFClientAnchor(picturePosition.getDx1(),picturePosition.getDx2(),
					picturePosition.getDy1(),picturePosition.getDy2(),picturePosition.getCol1(),
					picturePosition.getRow1(),picturePosition.getCol2(),picturePosition.getRow2());
			drawing.createPicture(anchor, workbook.addPicture(byteArrayOutputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
		}
	}

03版是这个

public static void AddPictureToExcel03(HSSFWorkbook workbook,ByteArrayOutputStream byteArrayOutputStream,PicturePosition picturePosition) {
		int sheetNumber=workbook.getNumberOfSheets();
		for(int i=0;i<sheetNumber;i++) {
			HSSFSheet sheet=workbook.getSheetAt(i);
			HSSFPatriarch drawing=sheet.createDrawingPatriarch();
			HSSFClientAnchor anchor=new HSSFClientAnchor(picturePosition.getDx1(),picturePosition.getDx2(),
					picturePosition.getDy1(),picturePosition.getDy2(),picturePosition.getCol1(),
					picturePosition.getRow1(),picturePosition.getCol2(),picturePosition.getRow2());
			drawing.createPicture(anchor, workbook.addPicture(byteArrayOutputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
		}

5.测试代码

package com.stydy.test;

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

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;
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 ExcelTest {
	
	public static void main(String[] args) {
		String excelFile="D:\\test.xls";
		FileOutputStream fileOut=null;
		BufferedImage bufferedImage=null;
		ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
		try {
			bufferedImage=ImageIO.read(new File("D:\\2.png"));
			ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
			FileInputStream fileInputStream=new FileInputStream(excelFile);
			//XSSFWorkbook workbook=new XSSFWorkbook(fileInputStream);
			HSSFWorkbook workbook=new HSSFWorkbook(fileInputStream);
			PicturePosition picturePosition=new PicturePosition(0,0,255,255,(short)1,1,(short)4,8);
			//AddPictureToExcel(workbook, byteArrayOutputStream, picturePosition);
			AddPictureToExcel03(workbook, byteArrayOutputStream, picturePosition);
			fileOut=new FileOutputStream(excelFile);
			workbook.write(fileOut);
			System.out.print(excelFile+"  Success!");
		}catch (Exception e) {
	        throw new RuntimeException(e.getMessage());
		}finally {
			try {
				fileOut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

6.效果图如下

测试图片
测试图片
03xls文件效果
03xls文件测试
07xlsx文件测试效果
07xlsx文件

**参考此博客**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值