Java Excel Insert Picture

Source: Internet
Author: User

There is a Hssfpatriarch object in the POI, which is the top-level manager for Paint, and its createpicture (anchor, Pictureindex) method enables you to insert a picture in Excel. So to insert a picture in Excel, three steps can be done.

First, get the Hssfpatriarch object,

Second, the new Hssfclientanchor object,

Third, call the Createpicture method.

Implementation is very easy to achieve, if you want to do it is still a bit difficult. Here we first insert a picture:

Public class Excelimagetest {public staticvoidMain (string[] args) {FileOutputStream fileout=NULL; BufferedImage bufferimg=NULL; //first put the read in a bytearrayoutputstream, in order to produce ByteArray        Try{bytearrayoutputstream bytearrayout=NewBytearrayoutputstream (); Bufferimg= Imageio.read (NewFile ("f:/picture/Photo/John Doe/Small Zhao 11.jpg")); Imageio.write (bufferimg,"JPG", bytearrayout); Hssfworkbook WB=NewHssfworkbook (); Hssfsheet Sheet1= Wb.createsheet ("Test Picture"); //the top manager of paint, a sheet can only get one (be sure to note this)Hssfpatriarch Patriarch =Sheet1.createdrawingpatriarch (); //anchor is used primarily to set the properties of a pictureHssfclientanchor anchor =NewHssfclientanchor (0, 0, 255, 255, ( Short) 1, 1, ( Short) 5, 8); Anchor.setanchortype (3); //Insert PicturePatriarch.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg))               ; Fileout=NewFileOutputStream ("d:/test Excel.xls"); //writing to an Excel fileWb.write (fileout); System.out.println ("----The Excle file has been generated------"); } Catch(Exception e) {e.printstacktrace (); }finally{              if(Fileout! =NULL){                   Try{fileout.close (); } Catch(IOException e) {e.printstacktrace (); }              }          }      }  }  

The following results are performed:

As to why this is the result, mainly because Hssfclientanchor (0, 0, 255, 255, (short) 1, 1, (short) 5, 8) This constructor is caused, I will explain this constructor: Hssfclientanchor ( int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2); the meanings of each parameter are as follows:

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.

Here dx1, Dy1 defines where the image starts at the beginning of the cell, and DX2, Dy2 defines where the end cell ends. Col1, Row1 defines the start cell, col2, and Row2 defines the end cell.

Here are two different constructors created, from which we can clearly see the meanings and differences of the above eight parameters.

The above is a picture inserted, then the implementation of inserting more than one picture? In fact, it is very simple to construct a number of different Hssfclientanchor objects, control the eight parameters, as follows:

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, +);                         Insert Picture              patriarch.createpicture (Anchor1, Wb.addpicture (Bytearrayout.tobytearray (), Hssfworkbook.picture_type_ JPEG));             Patriarch.createpicture (Anchor2, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg));

The rest of the code has the following results:

Reproduced in: http://blog.csdn.net/chenssy/article/details/20524563

Java Excel Insert Picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.