0
votes

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.

enter image description here

And I dont have the option to add getHyperlink() method

enter image description here

but when I use the sheet it does appear, although appears as hyperlinks and is an array.

enter image description here

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!

1
I've just realised the answer in that question is using Apache POI and not JXL. So i guess the question is, how do I do that in JXL? - OhAye
Can you edit the question to reflect that? Also, it seems that the first code block is POI and the second one is JXL - golimar

1 Answers

0
votes

I just decided to use the Apache POI instead as that's where the getHyperlink method is. And it works like a charm. I've posted my code below in the hope someone finds it useful if they find themselves in the same situation in trying to change from JXL to Apache POI! Also if you're using .xslx then you need to use XSSF instead of HSSF (http://www.codejava.net/coding/how-to-read-excel-files-in-java-using-apache-poi)

 public String[] readUsernameFromExcel() {
        String ex[] = new String[10];
        try {

            FileInputStream fileInputStream = new FileInputStream("filepath.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet worksheet = workbook.getSheetAt(0);
            HSSFRow row1 = worksheet.getRow(2);
            HSSFCell cellA3 = row1.getCell(0);
            ex[0] = String.valueOf(cellA3.getNumericCellValue()).trim();

            HSSFRow row2 = worksheet.getRow(2);
            HSSFCell cellB3 = row2.getCell(1);
            ex[1] = cellB3.getStringCellValue();

            HSSFRow row3 = worksheet.getRow(1);

            HSSFCell cellC2 = row3.getCell(2);
            Hyperlink h = cellC2.getHyperlink();
            ex[2] = h.getAddress();

            System.out.println("A3: " + ex[0]);
            System.out.println("B3: " + ex[1]);
            System.out.println("C2: " + ex[2]);

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();}


return ex;

    }