CSV文件未在Excel中正确加载 [英] CSV file not loading correctly in Excel

查看:257
本文介绍了CSV文件未在Excel中正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过一些类似的问题,但没有解答我的问题。

I've seen a couple of similar questions but have not had one that answers my issue.

我有一个导出到CSV按钮,从我的数据库导出结果到CSV。

I have an export to CSV button which exports results from my database to CSV.

我不想保存文件,但我只想使用标题将 echo 内容导出到文件然后应在Excel(或类似产品)中打开。

I don't want to save the file, but I just want to use headers to export the echo content to a file which should then be opened in Excel (or similar product).

所有工作正常,但Excel不显示为分隔值,而是显示1行中的所有行逗号完整。我在其他地方找到了一个解决方案,我应该添加sep =,\r\\\
到第一行告诉Excel使用逗号作为分隔符,这使它工作伟大;但在其他产品中,它现在在第一行显示sep =,并继续输出的剩余部分。

All works fine but Excel does not appear to separate the values, but rather shows all rows in 1 row with the commas intact. I found a solution elsewhere where I should add "sep=,\r\n" to the first line to tell Excel to use commas as the delimiter, which then makes it work great; but, in other products it now shows the sep=, on the first line and continues with the remaining of the output.

这里是我使用的代码: / p>

Here is the code I'm using:

 header("Expires: 0");
 header("Cache-control: private");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Content-Description: File Transfer");
 header("Content-Type: application/vnd.ms-excel");
 header("Content-disposition: attachment; filename=export.csv");
 echo "sep=,\r\n"; // This makes it work in excel but fails in other products such as openoffice
 echo "this,is,just,a,test\r\n";
 exit;


推荐答案

正如Mark Ba​​ker建议的,区域设置,我将其更改为;它工作像一个魅力:)谢谢!

As Mark Baker suggested, as the dilimeter is set by locale settings, I changed it to ; and it worked like a charm :) Thank you!

这篇关于CSV文件未在Excel中正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆