如何为 Excel 生成没有错误消息的 .csv 文件?

How to generate .csv file for Excel without error messages?

文件是通过 a 标签在 javascript 和 downloaded 中创建的。 但是 Excel 抱怨文件格式有问题。

  1. 什么文件格式 Excel 不会抛出错误?
  2. 这可以用 javascript 生成吗?

显然Excel wants the BOM at the beginning, but I have no idea how to change that. The file is encoded in Base64

第一个错误: 'filename.csv' 的文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非您信任它的来源,否则不要打开它。还是要打开吗?

是之后的第二个错误: Excel 检测到 'filename.csv' 是 SYLK 文件,但无法加载它。文件有错误或不是 SYLK 文件格式。单击“确定”尝试以其他格式打开文件。

您的输出文件是否以 'ID' 开头?如果是这样,则在文本编辑器中打开 csv 并进行更改。 有关详细信息,请参阅 http://support.microsoft.com/en-us/kb/215591

编辑:

那个 link 现在已经死了 - 但是使用它说的 wayback 机器:

MORE INFORMATION A SYLK file is a text file that begins with "ID" or "ID_xxxx", where xxxx is a text string. The first record of a SYLK file is the ID_Number record. When Excel identifies this text at the beginning of a text file, it interprets the file as being a SYLK file. Excel attempts to convert the file from the SYLK format, but is unable to because there are no valid SYLK codes following the "ID" characters. Because it cannot convert the file, Excel generates the error.

https://support.microsoft.com/en-us/help/170245/prj-files-saved-as-csv-can-t-be-opened-in-microsoft-excel

这是另一个 link 来解释它 - 不确定这个 link 会持续多久所以这里有一个屏幕截图以供将来参考:

运行 在为客户创建 CSV 文件时遇到这个恼人的问题。如果您是编写 CSV 文件并在 Excel 中打开它们的人,而不是 "ID",请使用 "Id".