Java POI Implementation inserts a picture into Excel

Source: Internet
Author: User

Java POI Implementation inserts a picture into ExcelTags: javapoiexcel2014-03-05 08:59 9103 People read Comments (4) favorite reports Classification:"Java Development"-----Javascore

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Doing web development inevitably involves dealing with Excel. Boss, give me a job today.-Export Excel. Start to think is pretty simple, nothing is to find, build excel,response download can. But there is a little different, is to add the picture, is this to add a picture for a long time. At the same time on the network did not find the better information, so write this blog post records, for their own and Bo friends query, reference.

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 Hssfpatriarch object, second, new Hssfclientanchor object, third, call Createpicture method can. Implementation is very easy to achieve, if you want to do it is still a bit difficult. Here we first insert a picture:

[Java]View Plaincopyprint?
  1. Public class Excelimagetest {
  2. public static void Main (string[] args) {
  3. FileOutputStream fileout = null;
  4. BufferedImage bufferimg = null;
  5. //First put the read in a bytearrayoutputstream in order to produce ByteArray
  6. try {
  7. Bytearrayoutputstream bytearrayout = new Bytearrayoutputstream ();
  8. bufferimg = Imageio.read (new File ("f:/picture/Photo/John Doe/Small Zhao 11.jpg"));
  9. Imageio.write (bufferimg, "jpg", bytearrayout);
  10. Hssfworkbook WB = new Hssfworkbook ();
  11. Hssfsheet Sheet1 = wb.createsheet ("Test Picture");
  12. //Paint top manager, one sheet can only get one (be sure to note this)
  13. Hssfpatriarch Patriarch = Sheet1.createdrawingpatriarch ();
  14. //anchor is used primarily to set the properties of a picture
  15. Hssfclientanchor anchor = New Hssfclientanchor (0, 0, 255, 255, (short) 1, 1, (short)     C10>5, 8);
  16. Anchor.setanchortype (3);
  17. //Insert Picture
  18. Patriarch.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg));
  19. Fileout = new FileOutputStream ("d:/test Excel.xls");
  20. //write to Excel file
  21. Wb.write (fileout);
  22. System.out.println ("----excle file has been generated------");
  23. } catch (Exception e) {
  24. E.printstacktrace ();
  25. }finally{
  26. if (fileout! = null) {
  27. try {
  28. Fileout.close ();
  29. } catch (IOException e) {
  30. E.printstacktrace ();
  31. }
  32. }
  33. }
  34. }
  35. }

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:

In the next article I will provide an Excel generated generic template, support custom styles, titles, write pictures, etc.!!

Java POI Implementation inserts a picture into Excel

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.