poi往excel插入图片,以及删除插入后的图片

package com.herman.test;

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

import javax.imageio.ImageIO;

import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 
 * @author Herman
 *
 */
public class POITest {
	public static void main(String[] args) throws Exception {
		create();
		insertImg();
		insertImg2();
		deleteImg();
	}

	public static void create() {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("sheet1");
		for (int i = 0; i < 10; i++) {
			XSSFRow row = sheet.createRow(i);
			for (int j = 0; j < 10; j++) {
				XSSFCell cell = row.createCell(j);
				if (1 == i && 1 == j) {
					cell.setCellValue("herman hello");
				}
			}
		}

		try {
			FileOutputStream outputStream = new FileOutputStream("D:\\\\text1.xlsx");
			workbook.write(outputStream);
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void update() throws IOException {
		try {
			FileInputStream in = new FileInputStream("D:\\text1.xlsx");
			XSSFWorkbook workbook = new XSSFWorkbook(in);
			XSSFSheet sheet = workbook.getSheetAt(0);
			XSSFRow row = sheet.getRow(1);
			XSSFCell cell = row.getCell(1);
			String value = cell.getStringCellValue();
			System.out.println("B2的单元格内容为:" + value);
			cell.setCellValue("");
			FileOutputStream outputStream = new FileOutputStream("D:\\text1.xlsx");
			workbook.write(outputStream);
			outputStream.close();
		} catch (Exception e) {
		}
	}

	public static void insertImg() {
		FileOutputStream fileOut = null;
		BufferedImage bufferImg = null;
		// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
		try {
			FileInputStream in = new FileInputStream("D:\\text1.xlsx");
			XSSFWorkbook wb = new XSSFWorkbook(in);

			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			bufferImg = ImageIO.read(new File("D:\\\\1111.jpg"));
			ImageIO.write(bufferImg, "jpg", byteArrayOut);

			XSSFSheet sheet1 = wb.getSheetAt(0);
			// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
			XSSFDrawing patriarch = sheet1.createDrawingPatriarch();

			// anchor主要用于设置图片的属性
			XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 3, 6);
			anchor1.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
			// 插入图片 1
			patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));


			fileOut = new FileOutputStream("D:\\text1.xlsx");
			// 写入excel文件
			wb.write(fileOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void insertImg2() {
		FileOutputStream fileOut = null;
		BufferedImage bufferImg = null;
		// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
		try {
			FileInputStream in = new FileInputStream("D:\\text1.xlsx");
			XSSFWorkbook wb = new XSSFWorkbook(in);

			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			bufferImg = ImageIO.read(new File("D:\\\\1111.jpg"));
			ImageIO.write(bufferImg, "jpg", byteArrayOut);

			XSSFSheet sheet1 = wb.getSheetAt(0);
			// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
			XSSFDrawing patriarch = sheet1.createDrawingPatriarch();

			// anchor主要用于设置图片的属性
			XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 5, 8);
			anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
			// 插入图片 2
			patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
			

			fileOut = new FileOutputStream("D:\\text1.xlsx");
			// 写入excel文件
			wb.write(fileOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void deleteImg() {
		FileOutputStream fileOut = null;
		BufferedImage bufferImg = null;
		// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
		try {
			FileInputStream in = new FileInputStream("D:\\text1.xlsx");
			XSSFWorkbook wb = new XSSFWorkbook(in);
			XSSFSheet sheet1 = wb.getSheetAt(0);

			for(POIXMLDocumentPart dr:sheet1.getRelations()){
                if(dr instanceof XSSFDrawing){
                    XSSFDrawing drawing = (XSSFDrawing)dr;
                    List<XSSFShape> shapes = drawing.getShapes();
                    for(XSSFShape shape:shapes){
                    	if(shape instanceof XSSFPicture && shape!=null){
                            XSSFPicture picture = (XSSFPicture)shape;
                            if(picture == null){
                                continue;
                            }
                            try{
                                XSSFClientAnchor anchor2 = picture.getPreferredSize();
                                anchor2.setCol1(0);
                                anchor2.setCol2(0);
                                anchor2.setDx1(0);
                                anchor2.setDx2(0);
                                anchor2.setDy1(0);
                                anchor2.setDy2(0);
                                anchor2.setRow1(0);
                                anchor2.setRow2(1);
                            } catch (Exception e) {
							}
                    	}
                    }
                }
			}

			fileOut = new FileOutputStream("D:\\text1.xlsx");
			// 写入excel文件
			wb.write(fileOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/cyberHerman/article/details/103724211