DEV Community

loading...

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.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

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");
        urlLink.setAddress("http://www.google.com");

        //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");
        mailLink.setAddress("mailto:abc@outlook.com");

        //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");
        fileLink.setAddress("C:\\Users\\Administrator\\Desktop\\Report.doc");

        //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");
        linkToSheet.setAddress("Sheet2!B5");

        //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");
        uncLink.setAddress("\\\\192.168.1.108");

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

Ouput
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://www.google.com", true);

        //Set column width and row height/ 
        sheet.getColumns()[2].setColumnWidth(21);
        sheet.getRows()[4].setRowHeight(36);

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

Alt Text

Discussion (0)

pic
Editor guide