如何通过使用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());
}