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文件

**參考此博客**

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章