欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POI 实现Excel文件中点击超链接跳转到某sheet页某列某行的功能

程序员文章站 2022-06-25 11:36:26
...
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
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.hssf.util.HSSFColor;

public class test {


public static void main(String[] args) throws IOException {  
    /* !使用POI版本:3.10-FINAL*/  
      
    /* 建立新HSSFWorkbook对象*/  
    HSSFWorkbook wb = new HSSFWorkbook();  
      
            /* 建立新的sheet对象*/  
    HSSFSheet sheet = wb.createSheet("汇总页面");  
    HSSFRow row = sheet.createRow((short)0);  
      
    /* 连接跳转*/  
    HSSFCell likeCell = row.createCell((short)0);        
    Hyperlink hyperlink = new HSSFHyperlink(Hyperlink.LINK_DOCUMENT);  
        // "#"表示本文档    "明细页面"表示sheet页名称  "A10"表示第几列第几行  
    hyperlink.setAddress("#明细页面!A10");  
    likeCell.setHyperlink((org.apache.poi.ss.usermodel.Hyperlink) hyperlink);  
    // 点击进行跳转  
    likeCell.setCellValue("11");  
      
    /* 设置为超链接的样式*/  
    HSSFCellStyle linkStyle = wb.createCellStyle();  
    HSSFFont cellFont= wb.createFont();  
    cellFont.setUnderline((byte) 1);  
    cellFont.setColor(HSSFColor.BLUE.index);  
    linkStyle.setFont(cellFont);  
    likeCell.setCellStyle(linkStyle);  
      
    /* 建立第二个sheet对象*/  
    HSSFSheet sheet2 = wb.createSheet("明细页面");  //建立新的sheet对象  
      
    for (int i = 0; i < 30; i++) {  
        HSSFRow row2 = sheet2.createRow((short)i);  
        HSSFCell cell2 = row2.createCell((short)0);  
        cell2.setCellValue("测试第"+(i+1)+"行");  
    }  
      
    /* 输出文件*/  
    FileOutputStream fileOut = new FileOutputStream("D:\\汇总和明细.xls");  
    wb.write(fileOut);  
    fileOut.close();  
}  
}