DEV Community


Add Hyperlinks to Excel in Java

eiceblue profile image E-iceblue Product Family ・2 min read

In an Excel worksheet, you can create a hyperlink based on text or an image in any cell. Then, click that hyperlink to go to the specificed location. In this article, I’ll share with you how to add a hyperlink in a cell that links to a website, an email address, an ip address, an external file or a cell in anther worksheet, by using Free Spire.XLS for Java.

Add Spire.Xls.jar as dependency

Method 1: Download Free Spire.XLS for Java pack, unzip it and you’ll get Spire.Doc.jar file from the “lib” folder. Import the jar file in your project as a denpendency.

Method 2: If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.

        <groupId> e-iceblue </groupId>

Add text hyerplinks

import com.spire.xls.*;

public class InsertTextHyperlinks {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Add a link to B3 that links to a website
        HyperLink urlLink = sheet.getHyperLinks().add(sheet.getCellRange("B3"));
        urlLink.setTextToDisplay("Link to a website");

        //Add a link to E3 that links to an email address
        HyperLink mailLink = sheet.getHyperLinks().add(sheet.getCellRange("E3"));
        mailLink.setTextToDisplay("Link to an email address");

        //Add a link to B7 that links to an external file
        HyperLink fileLink = sheet.getHyperLinks().add(sheet.getCellRange("B7"));
        fileLink.setTextToDisplay("Link to an external file");

        //Add a link to E7 that links to a cell in antoher sheet
        HyperLink linkToSheet = sheet.getHyperLinks().add(sheet.getCellRange("E7"));
        linkToSheet.setTextToDisplay("Link to a cell in sheet2");

        //Add a link to B11 that links to an ip address
        HyperLink uncLink = sheet.getHyperLinks().add(sheet.getCellRange("B11"));
        uncLink.setTextToDisplay("Link to an ip address");

        //Save to file
        workbook.saveToFile("AddTextHyperlinks.xlsx", ExcelVersion.Version2013);

Alt Text

Add an image hyperlink

import com.spire.xls.*;

public class InsertImageHyperlink {

    public static void main(String[] args) {

        //Create a Workbook instance and get the first worksheet 
        Workbook wb = new Workbook();
        Worksheet sheet = wb.getWorksheets().get(0);

        //Insert an image to the cell C5
        String picPath = "C:\\Users\\Administrator\\Desktop\\home_button.png";
        ExcelPicture picture = sheet.getPictures().add(5,3,picPath);

        //Set a hyperlink for the image 
        picture.setHyperLink("Http://", true);

        //Set column width and row height/ 

        //Save the document 
        wb.saveToFile("AddImageHyperlink.xlsx", ExcelVersion.Version2016);

Alt Text

Discussion (0)

Editor guide