Apache POI Hyperlink

In this Apache POI tutorial, you will learn how to create a hyperlink in Excel using Java. You will also learn how to read hyperlinks using the Apache POI library.

The Apache POI is an open-source Java API library for manipulating the various Microsoft files format such as Excel, PowerPoint, and Word in Java.

Important Classes and Methods to Create and Read Hyperlinks – Apache POI

CreationHelper: It is an interface available under org.apache.poi.ss.usermodel package. All known implementation classes are HSSFCreationHelper, SXSSFCreationHelper, and XSSFCreationHelper.

Hyperlink: It is an interface under the package org.apache.poi.ss.usermodel and all known implementation classes are HSLFHyperlink, HSSFHyperlink, XSLFHyperlink, and XSSFHyperlink.

HyperlinkType: The enumeration value indicating the hyperlink types, i.e., whether it is a document, file, URL, email, or none. This Enum is present under the package org.apache.poi.common.usermodel

All available hyperlink-type properties with their description in HyperlinkType:

PropertiesDescription
NONENot a hyperlink
DOCUMENTLink to a place in this document
URLLink to a web page
FILELink to a file
EMAILLink to an E-mail address

getCreationHelper(): It is a method used to create hyperlinks helper object, It returns an object that handles instantiating concrete classes of the various instances one needs for HSSF and XSSF.

createHyperlink(): This method is used to create a Hyperlink object.

setAddress() / getAddress(): These methods are used to set and get the hyperlink address.

setHyperlink() / getHyperlink(): These methods are used to set and get the hyperlink object to/from Excel cells.

How to Create a Hyperlink in Excel?

Steps to create hyperlinks in Excel using Apache POI:

  1. Create an CreationHelper object using the getCreationHelper() method. eg- CreationHelper helper= wb.getCreationHelper();
  2. Create a row and cell in an Excel sheet. eg- sheet.createRow(0).createCell(0);
  3. Create a hyperlink object using the createHyperlink() method. eg- Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
  4. Set the link address using the setAddress() method. eg- link.setAddress(“https://javacodepoint.com/”);
  5. Finally, set the created link to the cell. eg- cell.setHyperlink(link);

Sample code snippet:

CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("Hyperlinks");
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellValue("URL Link");
Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
link.setAddress("https://javacodepoint.com/");
cell.setHyperlink(link);

How to Read a Hyperlink in Excel Using Java?

Steps to Read Hyperlinks in Excel using Apache POI:

  1. Read the sheet from Excel the workbook. eg- Sheet sheet = wb.getSheetAt(0);
  2. Read the Excel cell from the Excel sheet. eg- Cell cell = sheet.getRow(0).getCell(0);
  3. Get the hyperlink object using the getHyperlink() method. eg- Hyperlink link = cell.getHyperlink();
  4. Read the link address and type using the getAddress() , and getType() method. eg- link.getAddress();

Sample code snippet:

Sheet sheet = wb.getSheetAt(0);
Cell cell = sheet.getRow(0).getCell(0);
Hyperlink link = cell.getHyperlink();
if (link != null) {
	System.out.println("Hyperlink address: " + link.getAddress());
}

Create and Read Hyperlinks in Excel using Apache POI Example

In this example, we will create multiple types of hyperlinks and read the hyperlink in Java using Apache POI.

package com.javacodepoint.excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * This class demonstrates how to create Hyperlinks in Excel.
 */
public class CreateHyperlinksInExcel {

	/**
	 * Method to create Hyperlinks in Excel
	 * 
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void createHyperlinks(Workbook wb) throws FileNotFoundException, IOException {

		// create a helper object for hyperlinks creation
		CreationHelper createHelper = wb.getCreationHelper();

		// cell style for hyperlinks
		// by default hyperlinks are blue and underlined
		CellStyle hlink_style = wb.createCellStyle();
		Font hlink_font = wb.createFont();
		hlink_font.setUnderline(Font.U_SINGLE);
		hlink_font.setColor(IndexedColors.BLUE.getIndex());
		hlink_style.setFont(hlink_font);

		Sheet sheet = wb.createSheet("Hyperlinks");

		// Create URL link
		Cell cell = sheet.createRow(0).createCell(0);
		cell.setCellValue("URL Link");
		Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
		link.setAddress("https://javacodepoint.com/");
		cell.setHyperlink(link);
		cell.setCellStyle(hlink_style);

		// Create link to a file in the current directory
		cell = sheet.createRow(1).createCell(0);
		cell.setCellValue("File Link");
		link = createHelper.createHyperlink(HyperlinkType.FILE);
		link.setAddress("link1.xlsx");
		cell.setHyperlink(link);
		cell.setCellStyle(hlink_style);

		// Create an E-mail link
		cell = sheet.createRow(2).createCell(0);
		cell.setCellValue("Email Link");
		link = createHelper.createHyperlink(HyperlinkType.EMAIL);
		// Note: if subject contains white spaces, make sure they are url-encoded
		link.setAddress("mailto:[email protected]?subject=Hyperlinks");
		cell.setHyperlink(link);
		cell.setCellStyle(hlink_style);

		// Create a document link to a place in this workbook
		// create a new sheet and cell
		Sheet sheet2 = wb.createSheet("New Sheet");
		sheet2.createRow(0).createCell(0).setCellValue("New Cell");
		cell = sheet.createRow(3).createCell(0);
		cell.setCellValue("Worksheet Link");
		Hyperlink link2 = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
		link2.setAddress("'New Sheet'!A1");
		cell.setHyperlink(link2);
		cell.setCellStyle(hlink_style);

		// Saving the workbook to the .xlsx file
		try (FileOutputStream out = new FileOutputStream("excel_hyperlinks.xlsx")) {
			wb.write(out);
		}

		System.out.println("Hyperlinks created successfully in Excel file.");
	}

	/**
	 * Method to read hyperlink
	 */
	public static void readHyperlink(Workbook wb) {

		// Read the hyperlink available at A1 (row=0, col=0)
		Sheet sheet = wb.getSheetAt(0);
		Cell cell = sheet.getRow(0).getCell(0);
		Hyperlink link = cell.getHyperlink();
		if (link != null) {
			System.out.println("Hyperlink address: " + link.getAddress());
		}

	}

	// main method to test
	public static void main(String[] args) throws IOException {

		/*
		 * Creating a workbook for XLSX file using XSSFWorkbook, Use HSSFWorkbook for
		 * XLS File
		 */
		try (Workbook wb = new XSSFWorkbook()) {

			// create hyperlink in excel
			createHyperlinks(wb);

			// read hyperlink from excel
			readHyperlink(wb);

		}

	}

}

Output Excel (XLSX) file:

Apache POI Hyperlink Excel

Apache POI Maven Dependency

Use the following maven dependencies in your project pom.xml file:

<!-- Used for Excel 2003 or before (xls) -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.3</version>
</dependency>
   
<!-- Used for Excel 2007 or later (xlsx)  -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>2.20.0</version>
</dependency>

If you want the complete Apache POI setup, visit another article here: Download and Install Apache POI in Eclipse.

Conclusion

In this article, you have learned the creation of hyperlinks in an Excel file and Read the hyperlinks using the Apache poi library.

Continue reading, Apache POI Excel Cell Color in Java.

Reference: Apache POI

Related Articles:

You may also like this:

Share with friends

Leave a Comment

Your email address will not be published. Required fields are marked *