Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Check this....stackoverflow.com/q/15934650/624003 I think that will be helpful for you... –  Sankumarsingh Jun 23 at 7: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 at 12:37

1 Answer 1

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());
share|improve this answer
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 at 15:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.