Insert/Read Hyperlinks in Excel Using Java

<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>
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);
}
}
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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Collaborative Music App-Part 1: playing sounds

Formula-E Championship NFT

Learn Git in a scenario-based way

Life at GDG Jalandhar

How to build a shop for selling digital goods

Evernote —”How Do I Create An Excel List of Selected Notes?”

Normalization in DBMS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

Java/ Convert Excel to XPS

Database connectivity with java 🤯, using mysql…

Java static can be…

Build maven project with push to docker hub in same time