Insert/Read Hyperlinks in Excel Using Java

The hyperlinks in Excel documents allows people to jump between different worksheets or between different websites. This article will demonstrate how to add hyperlinks to Excel cells and how to read hyperlinks from an existing Excel document by using Free Spire.XLS for Java.

Installation (2 Method)
1# Download the Free Spire.XLS for Java and unzip it, then add the Spire.Xls.jar file to your project as dependency.

2# You can also add the jar dependency to maven project 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>3.9.1</version>
</dependency>
</dependencies>

Insert Hyperlinks:

import com.spire.xls.*;

public class InsertHyperlinks {

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 B1 that links to a website
HyperLink urlLink = sheet.getHyperLinks().add(sheet.getCellRange("B1"));
urlLink.setTextToDisplay("Home Page");
urlLink.setAddress("https://www.google.com/");

//Add a link to B2 that links to an email address
HyperLink mailLink = sheet.getHyperLinks().add(sheet.getCellRange("B2"));
mailLink.setTextToDisplay("Contact Us");
mailLink.setAddress("mailto: robert@outlook.com");

//Add a link to B3 that links to an external file
HyperLink fileLink = sheet.getHyperLinks().add(sheet.getCellRange("B3"));
fileLink.setTextToDisplay("Open My Report");
fileLink.setAddress("C:\\Users\\Administrator\\Desktop\\report.doc");

//Add a link to B4 that links to another worksheet
HyperLink linkToSheet = sheet.getHyperLinks().add(sheet.getCellRange("B4"));
linkToSheet.setTextToDisplay("Jump to sheet2");
linkToSheet.setAddress("Sheet2!B5");

//Add a link to B5 that links to an ip address
HyperLink uncLink = sheet.getHyperLinks().add(sheet.getCellRange("B5"));
uncLink.setTextToDisplay("Go to 192.168.1.108");
uncLink.setAddress("\\\\192.168.1.108");

//Set the default row height
sheet.setDefaultRowHeight(15f);

//Set the width of specific column
sheet.setColumnWidth(2,20f);

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

Read Hyperlinks:

import com.spire.xls.*;

public class ReadHyperlinks {

public static void main(String[] args) {

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

//Load the sample Excel file
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\InsertHyperlinks.xlsx");

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

//Loop through the cells within all located range
for (Object cell : sheet.getAllocatedRange()) {

CellRange cellRange = (CellRange) cell;
//Detect if a cell range has hyperlinks
if (cellRange.getHyperlinks().getCount() > 0) {

//Print out the cell containing a hyperlink and the link address
String certainCell = String.format("Cell[%d, %d] has a hyperlink: ", cellRange.getRow(), cellRange.getColumn());
System.out.println(certainCell + cellRange.getHyperlinks().get(0).getAddress());
}
}
}
}

Sharing Java Code