POI插入图片

引入的包

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 javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.hssf.util.HSSFColor;

测试类

public class MyPoi {

	public void test() {
		FileOutputStream fileOut = null;
		BufferedImage bufferImg = null;
		BufferedImage bufferImg1 = null;
		try {
			// 读取图片
			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
			bufferImg = ImageIO.read(new File("e:/nanjing.jpg"));
			bufferImg1 = ImageIO.read(new File("e:/nanjing.jpg"));
			ImageIO.write(bufferImg, "jpg", byteArrayOut);
			ImageIO.write(bufferImg1, "jpg", byteArrayOut1);
			// 创建excel
			HSSFWorkbook excelfile = new HSSFWorkbook();
			HSSFSheet excelsheet = excelfile.createSheet();

			excelfile.setSheetName(0, "aaa");
			// 设置图片位置
			HSSFPatriarch patriarch = excelsheet.createDrawingPatriarch();
			HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,
					(short) 1, 1, (short) 10, 20);
			HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 512, 255,
					(short) 2, 30, (short) 10, 60);
			patriarch
					.createPicture(anchor, excelfile.addPicture(
							byteArrayOut.toByteArray(),
							HSSFWorkbook.PICTURE_TYPE_JPEG));
			patriarch.createPicture(anchor1,
					excelfile.addPicture(byteArrayOut1.toByteArray(),
							HSSFWorkbook.PICTURE_TYPE_JPEG));
			// 输出到磁盘
			fileOut = new FileOutputStream("E:/1/t"
					+ System.currentTimeMillis() + ".xls");
			excelfile.write(fileOut);
			fileOut.close();
		} catch (FileNotFoundException e) {
			System.out.println(e.getLocalizedMessage());

		} catch (IOException e) {
			System.out.println(e.getLocalizedMessage());

		}
	}

	

	public void testChart() {

	}

	public static void main(String[] args) {
		new MyPoi().test();

	}

}
效果图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值