How to get hyperlink address from a cell in excel by using java?

雨燕双飞 提交于 2019-12-12 00:45:02

问题


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:

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.


回答1:


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

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());
}


来源:https://stackoverflow.com/questions/17987028/how-to-get-hyperlink-address-from-a-cell-in-excel-by-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!