Look first.
Code
//Create a workbookHssfworkbook Workbook =NewHssfworkbook (); //Create a sheetIsheet Sheet1 = workbook. Createsheet ("Sheet1"); //set column width, Excel column width each pixel is 1/256Sheet1. Setcolumnwidth (0, -* the); Sheet1. Setcolumnwidth (1, -* the); IRow Rowheader= Sheet1. CreateRow (0);//To create a table header rowRowheader.createcell (0, celltype.string). Setcellvalue ("Production number"); Rowheader.createcell (1, celltype.string). Setcellvalue ("Image"); DataTable DT= Sqlhelperpro.seachdata ("SELECT Top 3 prodid,ktl_pic from Long_tmppic"); if(dt. Rows.Count >0) { intRowline =1;//Starting from the second row (index starting at 0) foreach(DataRow DataRowinchdt. Rows) {IRow row=Sheet1. CreateRow (Rowline); //set row height, Excel row heights each pixel is 1/20Row. Height = the* -; //fill in the production numberRow. Createcell (0, celltype.string). Setcellvalue (datarow["PRODID"]. ToString ()); //to read a picture file into a string byte[] bytes = System.IO.File.ReadAllBytes (datarow["Ktl_pic"]. ToString ()); intpictureidx=workbook. AddPicture (Bytes,picturetype.jpeg); Hssfpatriarch Patriarch=(Hssfpatriarch) Sheet1. Createdrawingpatriarch (); //the position of the illustration hssfclientanchor (dx1,dy1,dx2,dy2,col1,row1,col2,row2) is explained later.Hssfclientanchor anchor =NewHssfclientanchor ( -,Ten,0,0,1, Rowline,2, Rowline +1); //Insert the image in the appropriate positionHssfpicture pict =(hssfpicture) patriarch. Createpicture (anchor, PICTUREIDX); Rowline++; }} npoidowmload (workbook);//download Excel
View Code
Parsing of parameters: Hssfclientanchor (int dx1,int dy1,int dx2,int dy2,int col1,int row1,int col2,int row2)
DX1: The position (x offset) of the left side of the image relative to the Excel lattice value is: 0~1023; that is, the position of the 10 offset is approximately 100 divided by 1023 of the width of the entire cell, which is probably one of the 10 points.
Dy1: The position (y offset) of the relative Excel lattice above the picture has the value: 0~256 principle.
DX2: The right side of the picture relative to the position of the Excel lattice (x offset) range value is: 0~1023; Principle Ibid.
Dy2: The position (y offset) of the relative Excel lattice below the picture has the value: 0~256 principle.
Col1 and Row1: The position of the upper-left corner of the picture, in Excel cell as a reference, and the analogy between the two values for (), then the position of the upper left corner of the image is the lower-right corner of the Excel table () cell (a,1) in the lower-right corner of the point.
Col2 and Row2: The position of the lower right corner of the picture, with Excel cells as a reference, and the analogy between the two values (2,2), then the lower right corner of the picture is the point (b,2) in the lower-right corner of the Excel table (2,2) cell.
The above is purely personal development experience welcome correction
Npoi usage can refer to official website (Chinese) http://tonyqus.sinaapp.com/
Npoi inserting a picture into an Excel specified cell