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.