mdashu
mdashu

Reputation: 225

How to link a image from excel file using POI

I am writting some text say "Pass" into excel file using cell.setCellValue("Pass"). Now I have to create a link to an image which locates at directory (C:\Users\UserName\DeskTop\image\test.jpg) from an excel.

When I click on text pass from excel file, then it should open the test.jpg image.

Please guide me/share to JAVA code to achieve this.

Thanks, Md Ashfaq

Upvotes: 0

Views: 2018

Answers (2)

Sankumarsingh
Sankumarsingh

Reputation: 10099

Ashfaq...Following is a method you can user for hyperlink the screenshot with the cell.

 public static void hyperlinkScreenshot(XSSFCell cell, String FileAddress){
    XSSFWorkbook wb=cell.getRow().getSheet().getWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);
    Hyperlink hp = createHelper.createHyperlink(Hyperlink.LINK_FILE);
    FileAddress=FileAddress.replace("\\", "/");
    hp.setAddress(FileAddress);
    cell.setHyperlink(hp);
    cell.setCellStyle(hlink_style);
}

for details check here.

Upvotes: 1

Vasim
Vasim

Reputation: 3143

Cell.Hyperlinks.Add Anchor:=Selection, Address:= _
        "C:\Users\UserName\DeskTop\image\test.jpg", TextToDisplay:="pass"

You can remove the setvalue as TextToDisplay can handle this...

Upvotes: 0

Related Questions