user883836
user883836

Reputation:

line break within data for Excel 2003

Using PHP to extract data from SQL and then creating a .csv file on the server for emailing/download by way of fputcsv. All works well other than trying to get a new line within a field in Excel (2003).

I get Product1Product2Product3 in the cell when I need

Product1
Product2
Product3

I have tried single quotes, double quotes, CRs, LFs and I am rapidly disapearing up my own backside.

So the question is - what character do I need to get into the CSV file to achieve this?

It has to work in Excel

[Solution] - the problem lay somewhere in passing the \r\n to Excel through the PHP fputcsv - I was unable to achieve this in any sort of fashion (plenty of appending the desired \r\n to my actual cell data e.g. Product1\r\n) The suggestion to use $lfcr = chr(10) . chr(13); worked first time around. I guess this was more of a PHP rather than an Excel question - thanks to all resonses.

Upvotes: 1

Views: 5098

Answers (2)

vascowhite
vascowhite

Reputation: 18440

This is more of an excel rather than a php question. What you put into the csv file needs to be understood by excel which is why \r\n will not work.

Use this

$lfcr = chr(10) . chr(13);

Then append $lfcr to the end of each line.

Upvotes: 6

oezi
oezi

Reputation: 51807

for linebreaks in a field in a csv-file you just have to surround the field with double quotes and prepend a = like in the following example. the linebreaks itself can be either \r\n or just \n:

id;product;price
1;iMac;2.99
2;="product
with
linebreaks";1.99
3;Bananaphone;999.99

Upvotes: 1

Related Questions