Apache POI - Hyperlink



This chapter explains how to add hyperlinks to the contents in a cell. Usually hyperlinks are used to access any web URL, email, or an external file.

The following code shows how to create hyperlinks on cells.

Example - Adding Hyperlink in a Spreadsheet

ApachePoiDemo.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ApachePoiDemo {
   public static void main(String[] args) throws Exception {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet("Hyperlinks");
      XSSFCell cell;
      CreationHelper createHelper = workbook.getCreationHelper();
      XSSFCellStyle hlinkstyle = workbook.createCellStyle();
      XSSFFont hlinkfont = workbook.createFont();
      hlinkfont.setUnderline(XSSFFont.U_SINGLE);
      hlinkfont.setColor(IndexedColors.BLUE.index);
      hlinkstyle.setFont(hlinkfont);

      //URL Link
      cell = spreadsheet.createRow(1).createCell((short) 1);
      cell.setCellValue("URL Link");
      XSSFHyperlink link = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.URL);
      link.setAddress("https://www.tutorialspoint.com/");
      cell.setHyperlink((XSSFHyperlink) link);
      cell.setCellStyle(hlinkstyle);

      //Hyperlink to a file in the current directory
      cell = spreadsheet.createRow(2).createCell((short) 1);
      cell.setCellValue("File Link");
      link = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.FILE);
      link.setAddress("cellstyle.xlsx");
      cell.setHyperlink(link);
      cell.setCellStyle(hlinkstyle);

      //e-mail link
      cell = spreadsheet.createRow(3).createCell((short) 1);
      cell.setCellValue("Email Link");
      link = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.EMAIL);
      link.setAddress("mailto:contact@tutorialspoint.com?" + "subject=Hyperlink");
      cell.setHyperlink(link);
      cell.setCellStyle(hlinkstyle);
      
      FileOutputStream out = new FileOutputStream(new File("example.xlsx"));
      workbook.write(out);
      out.close();
      workbook.close();
      System.out.println("hyperlink.xlsx written successfully");
   }
}

Output

It will generate an Excel file named example.xlsx in your current directory and display the following output on the command prompt.

example.xlsx written successfully

The example.xlsx file looks as follows −

Hyperlink
Advertisements