HSSF is a pure Java implementation of POI Engineering for Excel 97 (-2007) file operations
XSSF is a pure Java implementation of the POI Project for Excel OOXML (. xlsx) file operations
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.
Steps:
first, Get Hssfpatriarch object two, New Hssfclientanchor Object Three, call Createpicture method
For the previous office2007
Public classExcelimagetest { Public Static voidMain (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 (); } } } }}
Hssfclientanchor (0, 0, 255, 255, (short) 1, 1, (short) 5, 8) This constructor is caused, and I'll explain this constructor: hssfclientanchor (int dx1,int dy1,int D X2,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.
Implement inserting multiple pictures
New Hssfclientanchor (0, 0, 1023,100, (short) 1, 1, (short) 5, 8new 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));
OFFICE2007 (XML) after
PackageCOM.ORG.APACHE.POI.XSSF;ImportJava.awt.image.BufferedImage;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJavax.imageio.ImageIO;ImportOrg.apache.poi.xssf.usermodel.XSSFClientAnchor;Importorg.apache.poi.xssf.usermodel.XSSFDrawing;ImportOrg.apache.poi.xssf.usermodel.XSSFSheet;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; Public classstartpoiexcelwriterimg { Public Static voidMain (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 ("C:\\users\\huage\\desktop\\121231\\1466685286772.jpg")); Imageio.write (bufferimg,"JPG", bytearrayout); Xssfworkbook WB=NewXssfworkbook (); Xssfsheet Sheet1= Wb.createsheet ("Sheet1"); //xssfsheet Sheet1 = Wb.getsheet ("Sheet1"); //the top manager of paint, a sheet can only get one (be sure to note this)Xssfdrawing Patriarch =Sheet1.createdrawingpatriarch (); //anchor is used primarily to set the properties of a pictureXssfclientanchor anchor =NewXssfclientanchor (0, 0, 255, 255, ( Short) 1, 1, ( Short) 5, 8); Anchor.setanchortype (3); //Insert PicturePatriarch.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), xssfworkbook.picture_type_jpeg)) ; Fileout=NewFileOutputStream ("C:\\users\\huage\\desktop\\121231\\11111.xlsx"); //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 (); } } } } }
Spring-poi-excle writing a picture to a cell