Java calls Apache poi to insert an image into Excel

Source: Internet
Author: User

In Java, you can call Apache's poi operation EXCEL to insert an image into excel.

 

// Create a new workbook WB = new xssfworkbook (); // or new hssfworkbook (); // Add picture data to this workbook. // open the image inputstream is = new fileinputstream ("image1.jpeg"); byte [] bytes = ioutils. tobytearray (is); // Add the image to the workbook int pictureidx = WB. addpicture (bytes, workbook. picture_type_jpeg); is. close (); creationhelper helper = WB. getcreationhelper (); // create sheet = WB. createsheet (); // create the drawing patriarch. this is the top level container for all shapes. drawing drawing = sheet. createdrawingpatriarch (); // Add a picture shape clientanchor anchor = helper. createclientanchor (); // set top-left corner of the picture, // subsequent call of picture # resize () will operate relative to it // set the Image Location anchor. setcol1 (3); anchor. setrow1 (2); picture PICT = drawing. createpicture (anchor, pictureidx); // auto-size picture relative to its top-left corner Pict. resize (); // save workbook string file = "picture.xls"; if (WB instanceof xssfworkbook) file + = "X "; // output file fileoutputstream fileout = new fileoutputstream (File); WB. write (fileout); fileout. close ();

 

 

Link to this article: blog.csdn.net/joyous/article/details/8780112


Q group 236201801 Discussion

 

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.