在 Excel 中设置图片

package com.smbea.demo.excel;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

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;

/**
 * 在 Excel 中设置图片
 * @author hapday
 * @date 2017年1月20日 @time上午9:48:57
 */
public class ExcelImageTest {  
	/**
	 * 在 Excel 中设置图片
	 * 图片来源于网络
	 * 通过 HTTP 协议获取
	 */
	public static void excelSetImageForHTTP() {
		URL url = null;		// URL 地址
		
		try {
			url = new URL("http://images2015.cnblogs.com/blog/897247/201610/897247-20161012133845281-1066342716.png");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		InputStream  inputStream = null;	// 输入流

		try {
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();	// 打开 HTTP 连接
			
			int responseCode = httpURLConnection.getResponseCode();
			System.out.println("responseCode = " + responseCode);

			if(404 == responseCode) {
				System.out.println("文件未找到!");
				
				return;
			}
			
			if(200 != responseCode) {
				return ;
			} 
			
			inputStream = httpURLConnection.getInputStream();	// 获取 HTTP 连接中的输入流
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		ByteArrayOutputStream excelByteArrayOutputStream = new ByteArrayOutputStream();		// 创建字节数组输出流
		
		try {
			BufferedImage bufferedImage = ImageIO.read(inputStream);	// 将原图片读取到图片缓冲区中
			ImageIO.write(bufferedImage, "png", excelByteArrayOutputStream);	// 从图片缓冲区中的数据写入到字节数组输出流中
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();		// 创建一个工作簿,对应一个 Excel 文件
		HSSFSheet hssfSheet = hssfWorkbook.createSheet("image test");		// 在工作簿中创建一个工作表
		HSSFPatriarch hssfPatriarch = hssfSheet.createDrawingPatriarch();	// 在工作表中创建一个画布
//		HSSFClientAnchor hssfClientAnchor = new HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 8, 10);	// 在指定坐标创建锚点
		/**
		 * 第一个参数:左外边距
		 * 第二个参数:上外边距
		 * 第三个参数:右外边距
		 * 第四个参数:下外边距
		 * 第五个参数:起始行索引
		 * 第六个参数:起始列索引
		 * 第七个参数:结束行索引
		 * 第八个参数:结束列索引
		 */
		HSSFClientAnchor hssfClientAnchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 2, (short) 2, 3);	// 在指定坐标创建锚点
		hssfPatriarch.createPicture(hssfClientAnchor, hssfWorkbook.addPicture(excelByteArrayOutputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));	// 在画布上创建图片,指定具体的锚点、图片输出流、图片格式
		FileOutputStream excelFileOutputStream = null;		// Excel 文件输出流,用于向外部输出 Excel 文件
		try {
			excelFileOutputStream = new FileOutputStream("D:/excelImageTest.xls");		// 创建本地文件输出流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			hssfWorkbook.write(excelFileOutputStream);		// 将输出流写入到工作簿中
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("****** Excel 嵌入图片成功 *******");
		
		try {
			excelFileOutputStream.close();	// 关闭输出流
			hssfWorkbook.close();		// 关闭工作簿流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在 Excel 中设置图片
	 * 图片来源于网络
	 * 通过 HTTP 协议获取
	 */
	public static void excelSetImageForHTTP2() {
		FileOutputStream excelFileOutputStream = null;		// Excel 文件输出流,用于向外部输出 Excel 文件
		BufferedImage bufferedImage = null;	// 图片缓冲区,用于在内存中缓存图片,以提高程序性能
		URL url = null;		// URL 地址
		InputStream  inputStream = null;	// 输入流
		
		try {
			url = new URL("http://images2015.cnblogs.com/blog/897247/201610/897247-20161012133845281-1066342716.png");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		try {
			URLConnection urlConnection = url.openConnection();		// 打开 FTP 连接
			inputStream = urlConnection.getInputStream();	// 获取 FTP 连接中的输入流
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		ByteArrayOutputStream excelByteArrayOutputStream = new ByteArrayOutputStream();		// 创建字节数组输出流
		
		try {
			bufferedImage = ImageIO.read(inputStream);	// 将原图片读取到图片缓冲区中
			ImageIO.write(bufferedImage, "png", excelByteArrayOutputStream);	// 从图片缓冲区中的数据写入到字节数组输出流中
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();		// 创建一个工作簿,对应一个 Excel 文件
		HSSFSheet hssfSheet = hssfWorkbook.createSheet("image test");		// 在工作簿中创建一个工作表
		HSSFPatriarch hssfPatriarch = hssfSheet.createDrawingPatriarch();	// 在工作表中创建一个画布
		HSSFClientAnchor hssfClientAnchor = new HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 8, 10);	// 在指定坐标创建锚点
		hssfPatriarch.createPicture(hssfClientAnchor, hssfWorkbook.addPicture(excelByteArrayOutputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));	// 在画布上创建图片,指定具体的锚点、图片输出流、图片格式
		try {
			excelFileOutputStream = new FileOutputStream("D:/excelImageTest.xls");		// 创建本地文件输出流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			hssfWorkbook.write(excelFileOutputStream);		// 将输出流写入到工作簿中
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("****** Excel 嵌入图片成功 *******");
		
		try {
			excelFileOutputStream.close();	// 关闭输出流
			hssfWorkbook.close();		// 关闭工作簿流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在 Excel 中设置图片
	 * 图片来源于本地磁盘
	 */
	public static void excelSetPictureForLocal() {
		FileOutputStream excelFileOutputStream = null;		// Excel 文件输出流,用于向外部输出 Excel 文件
		BufferedImage bufferedImage = null;	// 图片缓冲区,用于在内存中缓存图片,以提高程序性能
		ByteArrayOutputStream excelByteArrayOutputStream = null;		// 字节数组输出流
		
		excelByteArrayOutputStream = new ByteArrayOutputStream();		// 创建字节数组输出流
		File imageSourceFile = new File("D:/网络7层示意图.jpg");
		
		try {
			bufferedImage = ImageIO.read(imageSourceFile);	// 将原图片读取到图片缓冲区中
			ImageIO.write(bufferedImage, "png", excelByteArrayOutputStream);	// 从图片缓冲区中的数据写入到字节数组输出流中
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();		// 创建一个工作簿,对应一个 Excel 文件
		HSSFSheet hssfSheet = hssfWorkbook.createSheet("image test");		// 在工作簿中创建一个工作表
		HSSFPatriarch hssfPatriarch = hssfSheet.createDrawingPatriarch();	// 在工作表中创建一个画布
		HSSFClientAnchor hssfClientAnchor = new HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 8, 10);	// 在指定坐标创建锚点
		hssfPatriarch.createPicture(hssfClientAnchor, hssfWorkbook.addPicture(excelByteArrayOutputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));	// 在画布上创建图片,指定具体的锚点、图片输出流、图片格式
		try {
			excelFileOutputStream = new FileOutputStream("D:/Excel中设置图片.xls");		// 创建本地文件输出流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			hssfWorkbook.write(excelFileOutputStream);		// 将输出流写入到工作簿中
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("****** Excel 嵌入图片成功 *******");
		
		try {
			excelFileOutputStream.close();	// 关闭输出流
			hssfWorkbook.close();		// 关闭工作簿流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
    public static void main(String[] args) {  
//    	excelSetImageForHTTP();
    	excelSetImageForHTTP2();
//    	excelSetPictureForLocal();
    }
}  

  

原文地址:https://www.cnblogs.com/hapday/p/6321973.html