"POI" uses POI to process hyperlinks and insert pictures in xlsx's cell

Source: Internet
Author: User

Use POI to insert hyperlinks and insert pictures into xlsx

1  Packagecom.it.poiTest;2 3 ImportJava.awt.image.BufferedImage;4 ImportJava.io.ByteArrayOutputStream;5 ImportJava.io.File;6 Importjava.io.FileNotFoundException;7 ImportJava.io.FileOutputStream;8 Importjava.io.IOException;9 Ten ImportJavax.imageio.ImageIO; One  A ImportOrg.apache.poi.common.usermodel.Hyperlink; - ImportOrg.apache.poi.hslf.examples.CreateHyperlink; - ImportOrg.apache.poi.hssf.usermodel.HSSFClientAnchor; the ImportOrg.apache.poi.hssf.usermodel.HSSFPatriarch; - ImportOrg.apache.poi.hssf.util.HSSFColor; - ImportOrg.apache.poi.ss.usermodel.Cell; - ImportOrg.apache.poi.ss.usermodel.ClientAnchor.AnchorType; + ImportOrg.apache.poi.ss.usermodel.CreationHelper; - Importorg.apache.poi.ss.usermodel.IndexedColors; + ImportOrg.apache.poi.ss.usermodel.Row; A ImportOrg.apache.poi.xssf.usermodel.XSSFCellStyle; at ImportOrg.apache.poi.xssf.usermodel.XSSFClientAnchor; - Importorg.apache.poi.xssf.usermodel.XSSFDrawing; - ImportOrg.apache.poi.xssf.usermodel.XSSFFont; - ImportOrg.apache.poi.xssf.usermodel.XSSFHyperlink; - Importorg.apache.poi.xssf.usermodel.XSSFPicture; - ImportOrg.apache.poi.xssf.usermodel.XSSFPrintSetup; in ImportOrg.apache.poi.xssf.usermodel.XSSFSheet; - ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; to  +  Public classAddphotourl { -  the      Public Static voidMain (string[] args) { *Xssfworkbook Workbook =NewXssfworkbook (); $         Try {Panax NotoginsengFileOutputStream out =NewFileOutputStream ("Addphotourl.xlsx"); -Xssfsheet sheet = workbook.createsheet ("1"); theXssfcellstyle style =Workbook.createcellstyle (); +Xssffont Font =Workbook.createfont (); A Font.setunderline (xssffont.u_double); the Font.setcolor (IndexedColors.RED.getIndex ()); + Style.setfont (font); -              $              $             /** - * Implement URL hyperlinks in cell -              */ theSheet.setcolumnwidth (2, 4000); -Row row = Sheet.createrow (2);WuyiCell cell = Row.createcell (2); theCell.setcellvalue ("Angel A Crowded Blog"); - Cell.setcellstyle (style); Wu              -              AboutCreationhelper Createhelper =Workbook.getcreationhelper (); $             //There are 4.5 types of class parameters that implement hyperlinks, such as url,file, etc. -Xssfhyperlink link =(Xssfhyperlink) Createhelper.createhyperlink (hyperlink.link_url); -Link.setaddress ("http://www.cnblogs.com/sxdcgaq8080/"); - cell.sethyperlink (link); A              +              the             /** - * Cell implementation file Hyperlink $              */ theXssfcellstyle Style2 =Workbook.createcellstyle (); theRow = Sheet.createrow (3); theCell = Row.createcell (2); theCell.setcellvalue ("New folder. txt"); -Xssffont Font2 =Workbook.createfont (); in Font2.setcolor (HSSFColor.YELLOW.index); the Style2.setfont (font2); the Cell.setcellstyle (style2); Aboutlink =(Xssfhyperlink) Createhelper.createhyperlink (hyperlink.link_file); theLink.setaddress ("f:/new text. txt");//wrong path, may be problematic the cell.sethyperlink (link); the              +              -             /** the * Cell Implementation Mailbox Hyperlink Note This address of the mailboxBayi              */ theXssfcellstyle Style3 =Workbook.createcellstyle (); therow = Sheet.createrow (4); -Cell = Row.createcell (2); -Cell.setcellvalue ("Angel's Mailbox"); theXssffont Font3 =Workbook.createfont (); the Font3.setcolor (HSSFColor.VIOLET.index); the Style3.setfont (FONT3); thelink =(Xssfhyperlink) Createhelper.createhyperlink (hyperlink.link_email); -Link.setaddress ("Mailto:[email protected]?" + "Subject=hyperlink"); the cell.sethyperlink (link); the              the     94              the             /** the * Cell implementation Insert Picture the              */98row = Sheet.createrow (5); AboutCell = Row.createcell (5); -Row.setheight (( Short) 1000);101             //top-level manager for Paint102Xssfdrawing Patriarch =Sheet.createdrawingpatriarch ();103             //Configure parameters for the Picture Manager104             //parameter 1 x-axis coordinates in the first cell the             //parameter 2 The y-coordinate of the first cell106             //parameter 3 x-axis coordinates in cell two107             //parameter 4 The y-coordinate of the first two cells108             //parameter 5109             //parameter 6 the             //parameter 7111             //parameter 8 theXssfclientanchor anchor =NewXssfclientanchor (100, 100, 255, 255, 13, 9, 14, 16);113 Anchor.setanchortype (anchortype.dont_move_do_resize); the              the              theBytearrayoutputstream Byteoutput =NewBytearrayoutputstream ();117             //read the picture information118BufferedImage Bufferimage =imageio.read (NewFile ("f:/cool music. png"));119             //to write a picture to the Bytearrayoutputstream -Imageio.write (Bufferimage, "PNG", byteoutput);121             //parameter 1 represents the position of the picture parameter 2 represents the picture source122 Patriarch.createpicture (anchor, Workbook.addpicture (Byteoutput.tobytearray (), XSSFWORKBOOK.PICTURE_TYPE_PN G));123             124              the             /**126 * Set Print Area127              */ -             //set the number of lines in the sheet to the number of rows to the first column129Workbook.setprintarea (0, 1, 9, 1, 10); the             //set the paper size131 Sheet.getprintsetup (). Setpapersize (xssfprintsetup.a4_papersize); the             //set whether gridlines are displayed133Sheet.setdisplaygridlines (true);134             //set whether gridlines are printed135Sheet.setprintgridlines (true);136             137             138 Workbook.write (out);139 out.close (); $}Catch(FileNotFoundException e) {141 e.printstacktrace ();142}Catch(IOException e) {143 e.printstacktrace ();144         }145     }146 147}
View Code

The effect is as follows:

Appendix 1: The Types of Hyperlinks: Constants for hyperlink

Appendix 2: Setting the effect of picture processing:

Anchor.setanchortype (anchortype.dont_move_do_resize);

In fact, these are the following meanings:

Appendix 3: If New Xssfclientanchor (100, 100, 255, 255, 13, 9, 14, 16); Where the picture is placed, if the coordinates of the lower-right corner of the picture are less than the coordinates of the upper-left corner, the situation will occur:

"POI" uses POI to process hyperlinks and insert pictures in xlsx's cell

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.