NPOI inserts an image into a specified cell in excel, and npoi inserts an image.

Source: Internet
Author: User

NPOI inserts an image into a specified cell in excel, and npoi inserts an image.
First look

Code

 

// Create a workbook HSSFWorkbook workbook = new HSSFWorkbook (); // create a sheet ISheet sheet1 = workbook. createSheet ("sheet1"); // set the column width. In excel, the column width is 1/256 sheet1.SetColumnWidth (0, 18*256); sheet1.SetColumnWidth (1, 18*256 ); IRow rowHeader = sheet1.CreateRow (0); // create the rowHeader of the header row. createCell (0, CellType. STRING ). setCellValue ("Production Order 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) {int rowline = 1; // from the second row (index starts from 0) foreach (DataRow datarow in dt. rows) {IRow row = sheet1.CreateRow (rowline); // you can specify the row Height. In excel, the row height is 1/20 Rows. height = 80*20; // enter the production order number row. createCell (0, CellType. STRING ). setCellValue (datarow ["PRODID"]. toString (); // read the image file into a string byte [] bytes = System. IO. file. readAllBytes (datarow ["KTL_PIC"]. toString (); int pictureIdx = workbook. addPicture (bytes, PictureType. JPEG); HSSFPatriarch patriarch = (HSSFPatriarch) Placement (); // HSSFClientAnchor (dx1, dy1, dx2, dy2, col1, row1, col2, row2) HSSFClientAnchor anchor = new HSSFClientAnchor (70, 10, 0, 0, 1, rowline, 2, rowline + 1 ); // Insert the image to the corresponding position. HSSFPicture pict = (HSSFPicture) patriarch. createPicture (anchor, pictureIdx); rowline ++ ;}} NPOIDowmLoad (workbook); // download excelView Code

Parameter Parsing: 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 cell is 0 ~ 1023; that is, the offset of the input 100 is approximately 100 of the width of the entire cell divided by 1023, which is about 10 points.

Dy1: the position (y offset) of the excel cell above the image ranges from 0 ~ 256 same as above.

Dx2: the position (x offset) of the image relative to the excel cell on the right is 0 ~ 1023; Same principle.

Dy2: the position (y offset) of the image relative to the excel cell is 0 ~ 256 same as above.

Col1 and row1: the position in the upper-left corner of the image. Take the excel cell as a reference. The two values are (). The position in the upper-left corner of the image is the excel table) point in the lower-right corner of A cell (A, 1.

Col2 and row2: the position in the lower-right corner of the image. Take the excel cell as a reference. The two values are (). The position in the lower-right corner of the image is the excel table) point in the lower-right corner of a cell (B, 2.

The above is purely personal development experience. Please confirm it.

NPOI usage can refer to the official website (Chinese) http://tonyqus.sinaapp.com/

 

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.