Try   HackMD

CSV 如何顯示文本跟超連結(雙引號/逗號)


tags: csv Excel 匯出

Change display name for hyperlink in csv file while opening through Excel

Can Excel interpret the URLs in my CSV as hyperlinks?

問題排除
解決office(含word/excel)內的超連結無法開啟

Excel內的超連結點開卻顯示\【無法開啟指定檔案\】

CSV是純文本格式
在WindowOS下會預設使用Excel開啟
但除了文本還可以加入超連結寫法

怎麼使用CSV製作超連結??

加入超連結

=HYPERLINK("https://www.google.com/")

QA一定要看

使用講解

CSV需要將一個單元格中的雙引號(")用成兩個雙引號("")
所以程式會是

  • 第一行是原本PHP字串變數串接方法 但不建議這樣用

(三四五行一起使用)

  • 第三行是前後字串使用 . 包變數(前後字串中各有一個爽引號)
  • 第四行是 在用替換方式將一個引號換成兩個引號
  • 第五行是 使用引號跟逗號的單元個前後在夾雙引號
$content = "=HYPERLINK("{$link}")"; var_dump($content); $content = "=HYPERLINK(\"".$link."\")"; $content = str_replace("\"", "\"\"", $content); $content = "\"" . $content . "\"";

QA

問題

  1. 用Excel打開CSV為什麼超連結沒有變色跟下底線
  2. 如果想加入顏色可以嗎?(文字顏色或單元格顏色)
  3. 如何設定顯示預設文字??
  4. 點下超連結時遇到(在最後)
    • 無法開啟<URL>。找不到網際網路伺服器或 proxy 伺服器。
    • 無法開啟<URL>。無法開啟指定的檔案。
    • <URL> = 您插入超連結。

Q1+Q2

這是完成的CSV用Excel開啟的顯示
圖片上傳裡的單元格是超連結
但沒有下底線跟呈現超連結中的藍色

Q1 用Excel打開CSV為什麼超連結沒有變色跟下底線
原因是
其實Excel並沒有吃到超連結設定所以在Excel的程式中當然不會呈現
Excel程式所設定的樣式style

個人理解
請將Excel當成濾鏡 文件還是CSV格式
我們是透過Excel去看副檔名/擴展名為CSV的文件
而文件本身是不包含樣式style的

所以Excel呈現出來當然不會帶樣式

Q2 如果想加入顏色可以嗎?(文字顏色或單元格顏色)
無法加入顏色跟樣式
CSV本來就是純文字就是不含樣式
要使用樣式請使用第三方套件設定(或是htmlTable to Excel)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在Excel中製作超連結的差異

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Q3 如何設定顯示預設文字??

使用第二個參數

=HYPERLINK("https://www.google.com","Google")

實做案例

在串CSV時盡量像上面方式一樣 分開串雙引號/逗號
不要使用第一行方式串接

while ($row = SQLdataArray($stmt)) { $row['uname'] = Regreplace($row['uname']); $row['fbook'] = Regreplace($row['fbook']); $row['line'] = Regreplace($row['line']); $row['ig'] = Regreplace($row['ig']); $row['award'] = Regreplace($row['award']); $regTime = '' . $row['createTime']->format('Y-m-d H:i:s'); foreach($row as $k => $v){ if( in_array($k, $titleArr, true) ){ $str = ($v === '' || is_null($v) )? '':$v; if($k === 'cel' || $k === 'tel' || $k === 'orders' || $k === 'invoice' || $k === 'vip'){ $str = "\t{$str}"; } elseif($k === 'uploadImg'){ // 這段 $link = $url.$str; $cell = "=HYPERLINK(\"".$link."\", \"".$str."\")"; $cell = str_replace("\"", "\"\"", $cell); $cell = "\"" . $cell . "\""; $str = $cell; // $str = "<a href=\"{$link}\">{$link}</a>"; //確認不行 // $str = "=HYPERLINK(".$link.")"; // $str = "=HYPERLINK(\"" .$url . $str ."\")"; //"=HYPERLINK(""http://www.google.com"")" } $content .= "{$str},"; } } $content .= "{$regTime}\r\n"; }

Q4點下超連結時遇到以下問題

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →