简体   繁体   English

通过PHP的CSV导出未分离到MS Excel上的列

[英]CSV Export via PHP isn't separating to columns on MS Excel

I can't for the life of me figure this out at the moment but watch it be something blatantly obvious. 我目前无法解决这个问题,但请注意这是显而易见的。

CSV Exports are working and are comma delimited. CSV导出有效且以逗号分隔。 But MS Excel is refusing to column them correctly. 但是MS Excel拒绝对它们进行正确列。

I have the headers like so: 我有这样的标题:

header('Content-Encoding: UTF-8');
header("Content-type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=" . $filename . ".csv");

And the code like so: 像这样的代码:

$contents = "Artist,Title,Presenter,Time Played\n";

while ($row = mysql_fetch_array($user_query)) {
    $contents.= "\"" . $row['artist'] . "\",";
    $contents.= "\"" . $row['title'] . "\",";
    $contents.= "\"" . $row['presenter'] . "\",";
    $contents.= "\"" . $row['timeplayed'] . "\"\n";
}

$contents_final = chr(255) . chr(254) . mb_convert_encoding($contents, "UTF-16LE", "UTF-8");
print $contents_final;

And it seems to work fine on my MS Excel machine, but I have used this machine for a lot of manual CSV work so it may be a change I've made there. 它在我的MS Excel机器上似乎可以正常工作,但是我在很多手动CSV工作中使用了这台机器,因此这可能是我在此处所做的更改。 For first installs, it's not separating by columns properly. 对于首次安装,它没有正确按列分隔。

It still needs to be a CSV file though as a requirement. 尽管仍然需要,但仍需要是CSV文件。 Any thoughts on how I can force Excel to read this properly without the user having to manually force the columns? 关于如何可以强制Excel正确阅读而无需用户手动强制列的任何想法?

对于单独的列,请使用“ \\ t”;对于单独的行,请使用“ \\ n”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM