Insert/Read Hyperlinks in Excel Using Java

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

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

//Add a link to B3 that links to an external file
HyperLink fileLink = sheet.getHyperLinks().add(sheet.getCellRange("B3"));
fileLink.setTextToDisplay("Open My Report");

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

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

//Set the default row height

//Set the width of specific column

//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

//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