做Web開發免不了要與Excel打交道。今天老大給我一個任務-導出Excel。開始想的還是蠻簡單的,無非就是查找,構建Excel,response下載即可。但是有一點不同,就是要加入圖片,就是這個加入圖片搞了好久。同時網絡上確實沒有發現比較好的資料,所以寫這篇博文記錄之,供自己和博友們查詢,參考。
在POI中有HSSFPatriarch對象,該對象為畫圖的頂級管理器,它的createPicture(anchor, pictureIndex)方法就能夠在Excel插入一張圖片。所以要在Excel中插入圖片,三步就可以搞定。一、獲取HSSFPatriarch對象,二、new HSSFClientAnchor對象,三、調用createPicture方法即可。實現倒是非常容易實現,如果想把它做好還是有點兒難度的。這里我們先插入一張圖片:
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 </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> HSSFWorkbook(); HSSFSheet sheet1 </span>= wb.createSheet("test picture"<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">畫圖的頂級管理器,一個sheet只能獲取一個(一定要注意這點)</span> HSSFPatriarch patriarch =<span style="color: #000000"> sheet1.createDrawingPatriarch(); </span><span style="color: #008000">//</span><span style="color: #008000">anchor主要用於設置圖片的屬性</span> HSSFClientAnchor anchor = <span style="color: #0000ff">new</span> HSSFClientAnchor(0, 0, 255, 255,(<span style="color: #0000ff">short</span>) 1, 1, (<span style="color: #0000ff">short</span>) 5, 8<span style="color: #000000">); anchor.setAnchorType(</span>3<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">插入圖片 </span>
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對象,控制好那八個參數,如下:
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);</span><span style="color: #008000">//</span><span style="color: #008000">插入圖片 </span>
patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
其余代碼一樣,得到如下結果:
下篇我將提供一個Excel生成的通用模板,支持自定樣式、標題、寫入圖片等!!