How to encode a hyperlink in CSV formatted file?

2019-01-23 23:35发布

问题:

When I try to encode a HTML anchor link in CSV file cell it becomes corrupted and not readable by Excel.

Is there some sort of non-HTML solution or format to encode a hyperlink in CSV file cell?

回答1:

A CSV file is simply text - it's up to the loading program how it chooses to interpret the text.

If Excel is complaining when you feed it "<a href=\"blah\">Link</a>", "another cell" then try just having the raw URL and you might find Excel will automagically turn it into a link.

But in general Excel doesn't process HTML, so expecting it render HTML from a CSV file is asking too much.



回答2:

For when automagicalism doesn't work, and you're definitely using Excel, use this as the field content.

=HYPERLINK("http://stackoverflow.com")


回答3:

What worked for me in Excel 2003 - output to your CSV the statement:

CELLVALUE="=HYPERLINK("+QM+URLCONTENTS+QM+";"+QM+"URLDISPLAYNAME"+QM+")" 
  • note the semicolon ; use in the hyperlink. I've found the comma not to work for me in Excel 2003.
  • Depending on the script or language you use quotemarks could be handled differently. The cellvalue you put into the CSV before you import it into Excel should look exactly like this: "=HYPERLINK("URLCONTENTS";"URLDISPLAYNAME")"

where:

  • CELLVALUE is the output written to the CSV
  • QM is the ASCII value of ["] -> (ASCII 34)
  • URLCONTENTS is the full URL to the page you want to link to. -URLDISPLAYNAME is the text you see in the Excel cell.
You can also use relative paths and set a base location in Excel.

File/Properties > Tab Summary > Field Hyperlink Base.

Use as fieldvalue something like http://www.SITENAME.com/SUB_LOCATION/../SUB_LOCATION that sets your starting point so you can click it in Excel. Of course, you don't have to use SUB_LOCATIONs if the sitename itself already will resolve successfully for your relative path.

What I couldn't find is how to make the links automatically underlined in Excel. From other tips found in this article:

Format manually all linkcells as underlined and darkblue (for example) and then the standard functionality appears with already visited links turning into another color.



回答4:

This worked for me:

  1. Use the =HYPERLINK function, the first parameter is the web link, the second is the cell value.
  2. Put " quotes around the entire function.
  3. Escape the internal quotes within the function with two sets of quotes, i.e., ""

Here's a four-column comma-delimited example.csv:

5,6,"=HYPERLINK(""http://www.yahoo.com"";""See Yahoo"")",8

When a spreadsheet program (LibreOffice, etc.) opens this .csv, it creates an active link for you.