导出为 CSV:为什么 '\t' 不起作用,而 '\n' 起作用:

Export to CSV: Why '\t' is not working, while '\n' works:

所有内容都已下载,但“\t”无法正常工作,而“\n”可以正常工作并在 Excel 中正确呈现。

  $(document).on('click', 'button', function () {
    $.getJSON("http://localhost:8080/peace_reports/data.php", function (res) {
      var uri = 'data:text/csv;charset=utf-8,' + encodeURI(ddd());
        function ddd (){
            var ll = "";
            for (var i = 0; i < res.length; i++){
                ll += res[i]['Name'] + '\t' + res[i]['ID'] + '\n';
            }
            return ll;
        }

      //this trick will generate a temp <a /> tag
      var link = document.createElement("a");
      link.href = uri;

      //set the visibility hidden so it will not effect on your web-layout
      link.style = "visibility:hidden";
      link.download = "test" + ".csv";

      //this part will append the anchor tag and remove it after automatic click
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  });

您正在创建 .tsv 文件,而不是 .csv。 CSV 以逗号分隔,TSV 以制表符分隔。 https://en.wikipedia.org/wiki/Tab-separated_values

这可能就是它没有在 Excel 中正确显示的原因。尝试切换为逗号或更改文件类型。我建议切换到逗号,因为我认为 TSV 是一种非常罕见的文件格式。

csv格式可以加"\t,"

希望有用....