1. 程式人生 > >java POI實現向Excel中插入圖片

java POI實現向Excel中插入圖片

       做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 = 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物件,控制好那八個引數,如下:

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));

      其餘程式碼一樣,得到如下結果:


      下篇我將提供一個Excel生成的通用模板,支援自定樣式、標題、寫入圖片等!!