I am trying to read a hyperlink cell from my Excel spreadsheet but I'm unable to do so. When I go into the spreadsheet and remove the hyperlink it reads just fine. I did come across a solution below in another question (How to get hyperlink address from a cell in excel by using java?) but the getHyperlink method only works with the sheet and not the cell which is confusing me.
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());
}
This is my code just now
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public String[] readUsernameFromExcel() {
File src = new File("C:/filepath.xls");
String ex[] = new String[10];
try {
Workbook wb = Workbook.getWorkbook(src);
Sheet sh1 = wb.getSheet(0);
Cell a3 = sh1.getCell(0, 2);
Cell b3 = sh1.getCell(1,2);
Cell c2 = sh1.getCell(2,1); //this is the cell I want to read the hyperlink from
ex[0] = a3.getContents().trim();
ex[1] = b3.getContents().trim();
ex[2] = c2.getContents().trim();
System.out.println(ex[0]);
System.out.println(ex[1]);
System.out.println(ex[2]);
So what I have tried to do is
Hyperlink h = c2.getHyperlink();
But I getHyperlink does not work when used with a cell.
And I dont have the option to add getHyperlink() method
but when I use the sheet it does appear, although appears as hyperlinks and is an array.
I feel as though I'm so close but I just cant figure out what I'm missing or doing wrong so any help to get me over the link is greatly appreciated, thanks in advance!


