Java: Add Hyperlinks to Excel

Hyperlinks can direct readers from one file to a web address, an email address or another file. A hyperlink can be added to text or an image. In this article, we will introduce how to add hyperlinks to Excel in Java using Spire.XLS for Java library.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Add Text Hyperlinks to Excel in Java

The following are the steps to add a text hyperlink to Excel in Java:

  • Create an instance of Workbook class.
  • Get the desired worksheet using Workbook.getWorksheets().get() method.
  • Access the specific cell that you want to add hyperlink to using Worksheet.getRange().get() method.
  • Add a hyperlink to the cell using Worksheet.getHyperLinks().add() method.
  • Set the type, display text and address for the hyperlink using XlsHyperLink.setType(), XlsHyperLink.setTextToDisplay() and XlsHyperLink.setAddress() methods.
  • Autofit column width using XlsWorksheet.autoFitColumn() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

public class AddTextHyperlinks {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();

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

        //Add a text hyperlink that leads to a website
        CellRange cell1 = sheet.getRange().get("B3");
        HyperLink urlLink = sheet.getHyperLinks().add(cell1);
        urlLink.setType(HyperLinkType.Url);
        urlLink.setTextToDisplay("Link to a website");
        urlLink.setAddress("https://www.google.com/");

        //Add a text hyperlink that leads to an email address
        CellRange cell2 = sheet.getRange().get("E3");
        HyperLink mailLink = sheet.getHyperLinks().add(cell2);
        mailLink.setType(HyperLinkType.Url);
        mailLink.setTextToDisplay("Link to an email address");
        mailLink.setAddress("mailto:abc@outlook.com");

        //Add a text hyperlink that leads to an external file
        CellRange cell3 = sheet.getRange().get("B7");
        HyperLink fileLink = sheet.getHyperLinks().add(cell3);
        fileLink.setType(HyperLinkType.File);
        fileLink.setTextToDisplay("Link to an external file");
        fileLink.setAddress("C:\\Users\\Administrator\\Desktop\\Report.xlsx");

        //Add a text hyperlink that leads to a cell in another sheet
        CellRange cell4 = sheet.getRange().get("E7");
        HyperLink linkToSheet = sheet.getHyperLinks().add(cell4);
        linkToSheet.setType(HyperLinkType.Workbook);
        linkToSheet.setTextToDisplay("Link to a cell in sheet2");
        linkToSheet.setAddress("Sheet2!B5");

        //Add a text hyperlink that leads to a UNC address
        CellRange cell5 = sheet.getRange().get("B11");
        HyperLink uncLink = sheet.getHyperLinks().add(cell5);
        uncLink.setType(HyperLinkType.Unc);
        uncLink.setTextToDisplay("Link to a UNC address");
        uncLink.setAddress("\\\\192.168.0.121");

        //Autofit column width
        sheet.autoFitColumn(2);
        sheet.autoFitColumn(5);

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

Java: Add Hyperlinks to Excel

Add Image Hyperlinks to Excel in Java

The following are the steps to add an image hyperlink to Excel in Java

  • Create an instance of Workbook class.
  • Get the desired worksheet using Workbook.getWorksheets().get() method.
  • Insert an image into the worksheet using Worksheet.getPictures().add() method and set column width and row height.
  • Add a hyperlink to the image using XlsBitmapShape.setHyperLink() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class AddImageHyperlinks {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Insert an image into the worksheet
        ExcelPicture picture = sheet.getPictures().add(5, 3, "Logo.png");
        sheet.setRowHeight(5,60);
        sheet.setColumnWidth(3,11);

        //Add a hyperlink to the image
        picture.setHyperLink("https://www.e-iceblue.com", true);

        //Save the result file
        workbook.saveToFile("AddImageHyperlink.xlsx", ExcelVersion.Version2013);
    }
}

Java: Add Hyperlinks to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.