0

I'm trying to open all the files that are linked in an Excel Sheet using Apache POI. This is what I've got:

FileInputStream inputstream = new FileInputStream(file);
workbook = new HSSFWorkbook(inputstream);

HSSFSheet sheet = workbook.getSheetAt(0);
log("Processing Sheet: " + sheet.getSheetName());

Iterator rowIterator = sheet.iterator();

while (rowIterator.hasNext()) {
    HSSFRow row = (HSSFRow) rowIterator.next();
    Iterator cellIterator = row.cellIterator();

    while (cellIterator.hasNext()) {
        HSSFCell cell = (HSSFCell) cellIterator.next();

        if(cell.getHyperlink() != null){
            HSSFHyperlink hyperlink = cell.getHyperlink();
            log("Hyperlink found: " + hyperlink.getAddress());
            try{
                FileInputStream fs = new FileInputStream(hyperlink.getAddress());
            }catch(Exception ex){
                log(ex.getMessage());
            }

        }
    }
}

But hyperlink.getAddress() doesn't return the correct path, it looks like it returns the relative path but without ../.

I also tried using the getDirectoryRoot() method on the workbook. But that just returns /. I have the path to my Excel file but without the base path that the excel uses or the ../'s in the attachments path, I'm not able to get the correct path.

Frederik Voordeckers
  • 1,321
  • 15
  • 31
  • Check this....http://stackoverflow.com/q/15934650/624003 I think that will be helpful for you... – Sankumarsingh Jun 23 '14 at 07:26
  • @Sankumarsingh, thanks but that's for writing hyperlinks to the file. I need to read them and get the file from the hyperlink address. I can read the hyperlink address but I'm not able to open the file. The problem is that Excel uses relative paths... I can solve it by changing this setting to absolute but that is not exactly what I need... – Frederik Voordeckers Jun 23 '14 at 12:37

1 Answers1

0

Instead you can use one easier way: Take a file with that relative address and then get the absolute path of that file.

e.g.

System.out.println(new File(hyperlink.getAddress()).getAbsolutePath());
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
  • 1
    doesn't give the correct path either. Excel uses a really strange way of pointing to files if you do not change the settings ti absolute paths... – Frederik Voordeckers Jun 28 '14 at 15:40