POI给单元格添加超链接(xls,xlsx)

1、xls格式---(HSSF)

pom.xml配置

    <dependencies>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>


    </dependencies>

 java代码

package com.topcheer.html;

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 {
    
    @SuppressWarnings("deprecation")
    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);        
        org.apache.poi.ss.usermodel.Hyperlink hyperlink = new HSSFHyperlink(Hyperlink.LINK_DOCUMENT);  
            // "#"表示本文档    "明细页面"表示sheet页名称  "A10"表示第几列第几行  
        hyperlink.setAddress("#明细页面!A10");  
        likeCell.setHyperlink(hyperlink);  
        // 点击进行跳转  
        likeCell.setCellValue("1");  
          
        /* 设置为超链接的样式*/  
        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();  
    }  

}

2、xlsx格式---(XSSF & SXSSF)

package com.topcheer.html;

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

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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test2 {
    public static void main(String[] args) throws IOException {  
        /* !使用POI版本:3.10-FINAL*/  
          
        /* 建立新HSSFWorkbook对象*/  
        XSSFWorkbook workbook = new XSSFWorkbook();
          
        XSSFSheet createSheet = workbook.createSheet("汇总页面");  
        XSSFRow row = createSheet.createRow((short)0);  
          
        /* 连接跳转*/  
        XSSFCell likeCell = row.createCell((short)0);     
      //  XSSFHyperlink link = new XSSFHyperlink(XSSFHyperlink.LINK_URL);// 无法实例化XSSFHyperlink对象
        CreationHelper createHelper = workbook.getCreationHelper();
        XSSFHyperlink  hyperlink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
        // "#"表示本文档    "明细页面"表示sheet页名称  "A10"表示第几列第几行  
        hyperlink.setAddress("#明细页面!A10");  
        likeCell.setHyperlink(hyperlink);  
        // 点击进行跳转  
        likeCell.setCellValue("1");  
          
        /* 设置为超链接的样式*/  
        XSSFCellStyle linkStyle = workbook.createCellStyle();
        XSSFFont cellFont = workbook.createFont();
        cellFont.setUnderline((byte) 1);  
        cellFont.setColor(HSSFColor.BLUE.index);  
        linkStyle.setFont(cellFont);  
        likeCell.setCellStyle(linkStyle);  
          
        /* 建立第二个sheet对象*/  
        XSSFSheet sheet2 = workbook.createSheet("明细页面");  //建立新的sheet对象  
        for (int i = 0; i < 30; i++) {  
            XSSFRow row2 = sheet2.createRow((short)i);  
            XSSFCell cell2 = row2.createCell((short)0);  
            cell2.setCellValue("测试第"+(i+1)+"行");  
        }  
          
        /* 输出文件*/  
        FileOutputStream fileOut = new FileOutputStream("D:\\汇总和明细.xlsx");  
        workbook.write(fileOut);  
        fileOut.close();  
    }  

}

 

 

posted @ 2020-03-23 15:22  天宇轩-王  阅读(3732)  评论(1编辑  收藏  举报