0

I have a file which is stored in a server, the path is like: \\myserver\folder\myfile.txt

How to put this SMB address in the cell with hyperlink?

Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_FILE); 
link.setAddress("What to write here?");

When I used following code:

link.setAddress("\\\\myserver\\folder\\myfile.txt");

It returns error:

Caused by: java.net.URISyntaxException: Illegal character in path at index 0: \\myserver\folder\myfile.txt
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFHyperlink.validate(XSSFHyperlink.java:240)
... 2 more
cao
  • 259
  • 2
  • 11
  • What would you put into Excel for the same link? Then what happens if you give that to Apache POI? – Gagravarr Jun 19 '17 at 08:57
  • It should be what you need: https://stackoverflow.com/questions/7771888/access-to-file-using-java-with-samba-jcifs – The KNVB Jun 19 '17 at 09:06
  • No, that post is on how to deal with SMB access / authentication. I want to know how to add SMB hyperlink into an Excel cell via Apache POI. – cao Jun 19 '17 at 09:10
  • You need to encode that as a URL, try with `file:` and `/` not `\` as per the RFCs – Gagravarr Jun 19 '17 at 10:10

1 Answers1

3

What you're referencing is a UNC path. I don't know how UNC relates to SMB, but I successfully got the following code to work

CreationHelper createHelper = workbook.getCreationHelper();
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
link.setAddress((new File("\\\\myserver\\folder\\myfile.txt")).toURI().toString());
cell.setHyperlink(link);
Chris Maggiulli
  • 3,375
  • 26
  • 39