Apache POI Excel 创建超链接

Apache POI 允许我们在电子表格中创建超链接。在单元格中设置网址并在单击时重定向到服务器很有用。

请看,在下面的示例中,我们创建了重定向到 http://www.yiidian.com 的超链接

Apache POI 超链接示例

package com.yiidian;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;

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

public class HyperlinkFormula {
 public static void main(String[] args) throws IOException {
    try (HSSFWorkbook wb = new HSSFWorkbook()) {
         HSSFSheet sheet = wb.createSheet("new sheet");
         HSSFRow row = sheet.createRow(0);
         HSSFCell cell = row.createCell(0);
         cell.setCellType(CellType.FORMULA);
         cell.setCellFormula("HYPERLINK(\"http://www.yiidian.com/apache-poi\", \"点击我访问网站\")");
         try (FileOutputStream fileOut = new FileOutputStream("yiidian.xls")) {
             wb.write(fileOut);  
         }  
     }  
 }  
} 

输出结果为:

热门文章

优秀文章