Java inserts a picture into Excel

Source: Internet
Author: User

Java inserts a picture into Excel
  1. Import Java.io.FileOutputStream;
  2. Import Java.io.File;
  3. Import Java.io.ByteArrayOutputStream;
  4. Import java.io.IOException;
  5. Import Java.awt.image.BufferedImage;
  6. Import javax.imageio.*;
  7. Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. Import Org.apache.poi.hssf.usermodel.HSSFSheet;
  9. Import Org.apache.poi.hssf.usermodel.HSSFPatriarch;
  10. import Org.apache.poi.hssf.usermodel.HSSFClientAnchor;;
  11. Public class Testpoi {
  12. public static void Main (string[] args) {
  13. FileOutputStream fileout = null;
  14. BufferedImage bufferimg =null;
  15. BufferedImage BUFFERIMG1 = null;
  16. try{
  17. //First put the read in a bytearrayoutputstream in order to produce ByteArray
  18. Bytearrayoutputstream bytearrayout = new Bytearrayoutputstream ();
  19. Bytearrayoutputstream byteArrayOut1 = new Bytearrayoutputstream ();
  20. bufferimg = Imageio.read (new File ("d:/piechart.jpg"));
  21. BUFFERIMG1 = Imageio.read (new File ("d:/fruitbarchart.jpg"));
  22. Imageio.write (bufferimg,"jpg", bytearrayout);
  23. Imageio.write (BUFFERIMG1,"jpg", byteArrayOut1);
  24. //Create a workbook
  25. Hssfworkbook WB = new Hssfworkbook ();
  26. Hssfsheet Sheet1 = Wb.createsheet ("new sheet");
  27. //hssfrow row = Sheet1.createrow (2);
  28. Hssfpatriarch Patriarch = Sheet1.createdrawingpatriarch ();
  29. Hssfclientanchor anchor = New Hssfclientanchor (0,0,,255, (short) 1,1, (short)   ,20);
  30. Hssfclientanchor Anchor1 = new Hssfclientanchor (0,0,,255, (short) 2,   ),60);
  31. Anchor1.setanchortype (2);
  32. //Insert Picture
  33. Patriarch.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg));
  34. Patriarch.createpicture (Anchor1, Wb.addpicture (Bytearrayout1.tobytearray (), hssfworkbook.picture_type_jpeg));
  35. Fileout = new FileOutputStream ("D:/workbook.xls");
  36. //write to Excel file
  37. Wb.write (fileout);
  38. Fileout.close ();
  39. }catch (IOException io) {
  40. Io.printstacktrace ();
  41. System.out.println ("io Erorr:" + io.getmessage ());
  42. } finally
  43. {
  44. if (fileout! = null)
  45. {
  46. try {
  47. Fileout.close ();
  48. }
  49. catch (IOException e)
  50. {
  51. //TODO auto-generated catch block
  52. E.printstacktrace ();
  53. }
  54. }
  55. }
  56. }
  57. }

Java inserts a picture into Excel

Related Article

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.