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?
- Public class Excelimagetest {
- public static void Main (string[] args) {
- FileOutputStream fileout = null;
- BufferedImage bufferimg = null;
- //First put the read in a bytearrayoutputstream in order to produce ByteArray
- try {
- Bytearrayoutputstream bytearrayout = new Bytearrayoutputstream ();
- bufferimg = Imageio.read (new File ("f:/picture/Photo/John Doe/Small Zhao 11.jpg"));
- Imageio.write (bufferimg, "jpg", bytearrayout);
- Hssfworkbook WB = new Hssfworkbook ();
- Hssfsheet Sheet1 = wb.createsheet ("Test Picture");
- //Paint top manager, one sheet can only get one (be sure to note this)
- Hssfpatriarch Patriarch = Sheet1.createdrawingpatriarch ();
- //anchor is used primarily to set the properties of a picture
- Hssfclientanchor anchor = New Hssfclientanchor (0, 0, 255, 255, (short) 1, 1, (short) C10>5, 8);
- Anchor.setanchortype (3);
- //Insert Picture
- Patriarch.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg));
- Fileout = new FileOutputStream ("d:/test Excel.xls");
- //write to Excel file
- Wb.write (fileout);
- System.out.println ("----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:
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