poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Fisher <dfis...@jmlafferty.com>
Subject Re: Problem setting hyperlink to files outside of current directory
Date Fri, 14 Mar 2008 19:17:37 GMT
Hi -

I'm not sure, but it sounds like Excel is interpreting your first  
call as an http: URL link. Then when you use the dialog Excel gets  
smart enough to know that you are providing a Pathname.

Try:

             cell.setCellValue(new HSSFRichTextString 
("file:"+file.getName()));

Regards,
Dave

On Mar 14, 2008, at 1:53 PM, Faraz Rozi wrote:

> Hey everyone,
>
>   I'm having issues with hyper links. I am trying to set a link in  
> excel
> using the new setHyperlink method from HSSFCell, which I got from SVN
> checkout. I'm following online template for doing this but instead  
> of simply
> putting in the file name in String format I'm doing  
> file.getAbsolutePath()
> which also returns a String. For those wondering,  
> using .getCanonicalPath()
> produces the same results. This file variable holds the path to the  
> file I
> wish to link to. The problem is that while it looks like it creates  
> the
> hyper link in the excel sheet and displays the file name as the  
> link label,
> clicking on the link produces the following excel error message:
>
> The address of this site is not valid. Check the address and try  
> again.
>
> Here's where it gets interesting. If I select the hyper link and  
> choose
> EDIT, it correctly displays the path in the new dialog window. If I  
> choose
> OK, without changing ANYTHING, it fixes the link and I can now  
> click it and
> it will take me to the file! This almost feels like my previous  
> problem with
> evaluating formulas, where I had to re-evaluate formulas to get  
> appropriate
> answers. Is there a similar method for hyper links? Thanks guys.
>
>
> Here is the code:
>
> public void setFileLinkValue(File file, int row, int col)
>     {
>         try
>         {
>             HSSFCellStyle hlink_style = workBook.createCellStyle();
>             HSSFFont hlink_font = workBook.createFont();
>             hlink_font.setUnderline(HSSFFont.U_SINGLE);
>             hlink_font.setColor(HSSFColor.BLUE.index);
>             hlink_style.setFont(hlink_font);
>             HSSFRow sheetRow;
>             HSSFCell cell;
>             sheetRow = sheet.getRow(row);
>             cell = sheetRow.getCell((short) col);
>             HSSFHyperlink link = new HSSFHyperlink 
> (HSSFHyperlink.LINK_FILE);
>
>             //link to a file corresponding to row entry
>
>             cell.setCellValue(new HSSFRichTextString(file.getName()));
>             link.setAddress(file.getAbsolutePath());
>             cell.setHyperlink(link);
>             cell.setCellStyle(hlink_style);
>
>             outputStream = new FileOutputStream("C:\\test.xls");
>             workBook.write(outputStream);
>             outputStream.close();
>         }
>         catch (FileNotFoundException e)
>         {
>             System.out.println("file not found\n");
>             e.printStackTrace();
>         }
>         catch (IOException e)
>         {
>             System.out.println("IO Exception\n");
>         }
>     }


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message