English 中文(简体)
Apache POI - Hyperlink
  • 时间:2024-11-03

Apache POI - Hyperpnk


Previous Page Next Page  

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

The following code shows how to create hyperpnks on cells.


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

import org.apache.poi.common.usermodel.Hyperpnk;
import org.apache.poi.common.usermodel.HyperpnkType;
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.XSSFHyperpnk;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

pubpc class HyperpnkEX {
   pubpc static void main(String[] args) throws Exception {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet("Hyperpnks");
      XSSFCell cell;
      CreationHelper createHelper = workbook.getCreationHelper();
      XSSFCellStyle hpnkstyle = workbook.createCellStyle();
      XSSFFont hpnkfont = workbook.createFont();
      hpnkfont.setUnderpne(XSSFFont.U_SINGLE);
      hpnkfont.setColor(IndexedColors.BLUE.index);
      hpnkstyle.setFont(hpnkfont);

      //URL Link
      cell = spreadsheet.createRow(1).createCell((short) 1);
      cell.setCellValue("URL Link");
      XSSFHyperpnk pnk = (XSSFHyperpnk)createHelper.createHyperpnk(HyperpnkType.URL);
      pnk.setAddress("http://www.tutorialspoint.com/");
      cell.setHyperpnk((XSSFHyperpnk) pnk);
      cell.setCellStyle(hpnkstyle);

      //Hyperpnk to a file in the current directory
      cell = spreadsheet.createRow(2).createCell((short) 1);
      cell.setCellValue("File Link");
      pnk = (XSSFHyperpnk)createHelper.createHyperpnk(HyperpnkType.FILE);
      pnk.setAddress("cellstyle.xlsx");
      cell.setHyperpnk(pnk);
      cell.setCellStyle(hpnkstyle);

      //e-mail pnk
      cell = spreadsheet.createRow(3).createCell((short) 1);
      cell.setCellValue("Email Link");
      pnk = (XSSFHyperpnk)createHelper.createHyperpnk(HyperpnkType.EMAIL);
      pnk.setAddress("mailto:contact@tutorialspoint.com?" + "subject = Hyperpnk");
      cell.setHyperpnk(pnk);
      cell.setCellStyle(hpnkstyle);
      
      FileOutputStream out = new FileOutputStream(new File("hyperpnk.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println("hyperpnk.xlsx written successfully");
   }
}

Save the above code as HyperpnkEX.java. Compile and execute it from the command prompt as follows −


$javac HyperpnkEX.java
$java HyperpnkEX

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


hyperpnk.xlsx written successfully

The hyperpnk.xlsx file looks as follows −

Hyperpnk Advertisements