HSSF是POI工程對Excel 97(-2007)文件操作的純Java實現
XSSF是POI工程對Excel 2007 OOXML (.xlsx)文件操作的純Java實現
在POI中有HSSFPatriarch對象,該對象為畫圖的頂級管理器,它的createPicture(anchor, pictureIndex)方法就能夠在Excel插入一張圖片。
步驟:
一、獲取HSSFPatriarch對象 二、new HSSFClientAnchor對象 三、調用createPicture方法
針對office2007以前的
public class ExcelImageTest { public static void main(String[] args) { FileOutputStream fileOut = null; BufferedImage bufferImg = null; //先把讀進來的圖片放到一個ByteArrayOutputStream中,以便產生ByteArray try { ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(new File("F:/圖片/照片/無名氏/小昭11.jpg")); ImageIO.write(bufferImg, "jpg", byteArrayOut); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("test picture"); //畫圖的頂級管理器,一個sheet只能獲取一個(一定要注意這點) HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); //anchor主要用於設置圖片的屬性 HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8); anchor.setAnchorType(3); //插入圖片 patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); fileOut = new FileOutputStream("D:/測試Excel.xls"); // 寫入excel文件 wb.write(fileOut); System.out.println("----Excle文件已生成------"); } catch (Exception e) { e.printStackTrace(); }finally{ if(fileOut != null){ try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)這個構造函數造成的,下面我就來解釋這個構造函數:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各個參數的含義如下:
dx1:the x coordinate within the first cell。 dy1:the y coordinate within the first cell。 dx2:the x coordinate within the second cell。 dy2:the y coordinate within the second cell。 col1:the column (0 based) of the first cell。 row1:the row (0 based) of the first cell。 col2:the column (0 based) of the second cell。 row2:the row (0 based) of the second cell。
這里dx1、dy1定義了該圖片在開始cell的起始位置,dx2、dy2定義了在終cell的結束位置。col1、row1定義了開始cell、col2、row2定義了結束cell。
實現插入多張圖片
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8); HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16); //插入圖片 patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
office2007(xml)以后
package com.org.apache.poi.xssf; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; 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 StartPoiExcelWriterImg { public static void main(String[] args) { FileOutputStream fileOut = null; BufferedImage bufferImg = null; //先把讀進來的圖片放到一個ByteArrayOutputStream中,以便產生ByteArray try { ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(new File("C:\\Users\\huage\\Desktop\\121231\\1466685286772.jpg")); ImageIO.write(bufferImg, "jpg", byteArrayOut); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet1 = wb.createSheet("Sheet1"); //XSSFSheet sheet1 = wb.getSheet("Sheet1"); //畫圖的頂級管理器,一個sheet只能獲取一個(一定要注意這點) XSSFDrawing patriarch = sheet1.createDrawingPatriarch(); //anchor主要用於設置圖片的屬性 XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8); anchor.setAnchorType(3); //插入圖片 patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); fileOut = new FileOutputStream("C:\\Users\\huage\\Desktop\\121231\\11111.xlsx"); // 寫入excel文件 wb.write(fileOut); System.out.println("----Excle文件已生成------"); } catch (Exception e) { e.printStackTrace(); }finally{ if(fileOut != null){ try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
