Java Excel 插入圖片

來源:互聯網
上載者:User

標籤:column   fill   row   sfpa   font   close   --   new   tput   

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

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

 

轉載於:http://blog.csdn.net/chenssy/article/details/20524563

 

Java Excel 插入圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.