How to Create Hyperlink in Spreadsheet using Java?
Last Updated :
08 Jan, 2024
Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. It is an open-source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java programs. It contains classes and methods to decode the user input data or a file into MS Office documents.
Apache POI allows us to create hyperlinks in spreadsheets. It is useful to set the web address in the cell and redirect it to the server when clicked.
Approach for Creating Hyperlinks in SpreadSheet
- Import all the necessary .jar files like XSSF, XML and also you can add Maven Dependency in the maven project as:
<dependency>
<groupld>org.apache.poi</groupld>
<artifactld>poi</artifactld>
<version>3.9</version>
</dependency>
Eclipse
- Create an instance of the workbook
- Create a spreadsheet in the above workbook.
- Create rows using XSSFRow
- Create a cell using XSSFCell.
- Set hyperlink along with cell value. ting cell values.
- Writing the content to the workbook by defining the object of type FileOutputStream
- Close the connection of the file.
Procedure:
- Create a Maven project in Eclipse and add Apache POI(used for setting value type of cells) and also import all the necessary .jar files like HSSF, and XML.
- Give the name to the workbook.
- Create a workbook using “new XSSFWorkbook()” in which we have to create the spreadsheet.
- Create a spreadsheet in the workbook using “workbook.createSheet(‘Sheet1’)” and also give the name to the sheet as "Sheet1"
- Create a row using XSSFRow. Rows are 0-based.
- Create a cell using XSSFCell.
- Set a hyperlink using cell.setCellFormula() along with setting a value to a cell using cell.setCellValue();
- Place the output file in the default location and also kept in the try-catch block using FileOutputStream().
- Write it to the workbook created in the initial step using the workbook.write();
- Close the output file.
- Display a message on the console window when the program is successfully executed.
- Display an error message on the console window when the program is not successful this statement is kept in the catch block.
Example for Create Hyperlink in Spreadsheet using Java
Let's take an example to create a hyperlink in the cell that redirects to geeksforgeeks main page using Java.
Below is the implementation of the above approach:
Java
// Java Program to Apply Hyperlink to a Cell
// in a Spreadsheet Using Apache POI
// Importing required classes
import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// Main class
class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Name of WorkBook (custom input)
String excelfilename = "GeeksForGeeks.xlsx";
// Creating a WorkBook by
// creating an object of XSSFWorkbook class
XSSFWorkbook workbook = new XSSFWorkbook();
// Creating a Spread Sheet by creating an object of
// XSSFSheet and also give name
XSSFSheet spreadsheet
= workbook.createSheet("Sheet1");
// Creating a row
XSSFRow row = spreadsheet.createRow(1);
// Creating a cell and put a cell index value in it.
XSSFCell cell = row.createCell(1);
// Adding hyperlink along with cell value in it.
cell.setCellFormula(
"HYPERLINK(\"https://www.geeksforgeeks.org/\", \"click here\")");
// Try block to check for exceptions
try {
// Step 9
FileOutputStream outputfile
= new FileOutputStream(excelfilename);
workbook.write(outputfile);
// Closing connections using close() method
outputfile.close();
// Step 11
System.out.println(
excelfilename + " is written successfully");
}
// Catch block to handle exceptions
catch (FileNotFoundException e) {
// Step 12
System.out.println("ERROR!! " + e.getMessage());
}
}
}
Output: (On the console window)
1. When the program is successfully executed.
GeeksForGeeks.xlsx is written successfully.
2. When the program is not successfully executed.
ERROR!! GeeksForGeeks.xlsx
(The process cannot access the file because it is being used by another process)
Output: Workbook(excel file)
Similar Reads
How to Create Pivot Table in Excel using Java? A pivot table is needed to quickly analyze data of a table with very little effort (and no formulas) and sometimes not everyone has time to look at the data in the table and see whatâs going on and use it to build good-looking reports for large data sets in an Excel worksheet. Let's discuss a step-b
5 min read
How to Create Different Data Format of Cells in a Spreadsheet Using Java? Formatting in excel is a neat trick in excel which is used to change the appearance of the data represented in the worksheet, formatting can be done in multiple ways such as we can format the data of the cells by using the styles. By default, all worksheet cells are formatted with the General number
4 min read
How to Remove Hyperlinks in Excel? Different Methods Looking for the steps to remove unwanted hyperlinks form your Excel worksheet? Then in this short article we are going to discuss 6 different ways to remove hyperlinks in Excel. The steps discussed in this article work in all Excel versions from 2023 to the latest version of Excel.Actually, the HYPE
6 min read
How to Create a Formula in Excel using Java? Apache POI is a popular open-source Java library that provides programmers with APIs for creating, modifying, and editing MS Office files. Excel is very excellent at calculating formulas. And perhaps most Excel documents have formulas embedded. Therefore, itâs trivial that on a fine day, you have to
3 min read
How to Create Pivot Chart from Pivot Table in Excel using Java? A Pivot Chart is used to analyze data of a table with very little effort (and no formulas) and it gives you the big picture of your raw data. It allows you to analyze data using various types of graphs and layouts. It is considered to be the best chart during a business presentation that involves hu
4 min read
How to Insert a Picture in a Cell in MS Excel? Every day in business or any other field lots of information are there that are required to store for future use. For anyone, it is very difficult to remember that information for a long time. Earlier data and information are stored in a form of a register, file, or by paperwork but finding it may b
4 min read