如何通过使用Java从Excel中的单元格获取超链接地址? [英] How to get hyperlink address from a cell in excel by using java?

查看:159
本文介绍了如何通过使用Java从Excel中的单元格获取超链接地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用JavaExcelApi(jxl)或Apache POI通过编写一些Java代码来读取excel文件中单元格的字符串信息.但是现在我遇到了一个问题:

I know how to use JavaExcelApi (jxl) or Apache POI to read string information of a cell in an excel file by writing some java code. But now I got a problem:

一个单元格包含一个带有超链接的字符串.我可以在此单元格中读取字符串,但是我不知道如何通过java读取超链接地址.

A cell contains a string with a hyperlink on it. I can read the string in this cell, but I don't know how to read the hyperlink address through java.

推荐答案

您正在寻找的方法是

The method you're looking for is Cell.getHyperlink(), which returns either null (cell has no hyperlink) or a Hyperlink object

如果您想获取test.xls单元格B2的超链接URL,则可以执行以下操作:

If you wanted to fetch the hyperlink URL of cell B2 of test.xls, you'd do something like:

Workbook wb = WorkbookFactory.create(new File("test.xls"));
Sheet s = wb.getSheetAt(0);
Row r2 = s.getRow(1); // Rows in POI are 0 based
Cell cB2 = r2.getCell(1); // Cells are 0 based

Hyperlink h = cB2.getHyperlink();
if (h == null) {
   System.err.println("Cell B2 didn't have a hyperlink!");
} else {
   System.out.println("B2 : " + h.getLabel() + " -> " + h.getAddress());
}

这篇关于如何通过使用Java从Excel中的单元格获取超链接地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆