0

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. But MS Excel is refusing to column them correctly.

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. For first installs, it's not separating by columns properly.

It still needs to be a CSV file though as a requirement. Any thoughts on how I can force Excel to read this properly without the user having to manually force the columns?

2
  • try \r\n instead of just \n Commented Jan 12, 2014 at 19:24
  • @Dagon Still no change. However I think it turns out because the file isn't set to UTF-8 (or ANSI as UTF-8...). As converting it in Notepad++ forces it to then work in Excel. Sigh! Now to figure that out ... Commented Jan 13, 2014 at 12:32

1 Answer 1

1

For separate in columns use '\t' and for separate rows use '\n'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.