- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to add hyperlink to the contents of a cell using Java
Problem Description
How to add hyperlink to the contents of a cell using Java.
Solution
Following is the program to add hyperlink to the contents of a cell using Java.
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CreationHelper;
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 AddHyperlinkToCell {
public static void main(String[] args) throws Exception {
//Create a Workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a Spread Sheet
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(HSSFColor.BLUE.index);
hlinkstyle.setFont(hlinkfont);
//URL Link
cell = spreadsheet.createRow(1).createCell((short) 1);
cell.setCellValue("URL Link");
XSSFHyperlink link =
(XSSFHyperlink)createHelper.createHyperlink(Hyperlink.LINK_URL);
link.setAddress("http://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(Hyperlink.LINK_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(Hyperlink.LINK_EMAIL);
link.setAddress("mailto:contact@tutorialspoint.com?subject=Hyperlink");
cell.setHyperlink(link);
cell.setCellStyle(hlinkstyle);
FileOutputStream out = new FileOutputStream(
new File("C:/poiexcel/addHyperlink.xlsx"));
//Writing the content
workbook.write(out);
out.close();
System.out.println("hyperlink.xlsx written successfully");
}
}
Output
java_apache_poi_excel
Advertisements