printing a new line in a csv file cell
Asked Answered
C

2

23

I am generating an CSV file in php. I need to print the text in new line inside a cell(I can do this in excel using Ctrl+Enter or Alt+Enter). For that I tried using '\n', '\r', but nothing helped. How can I achieve this?

I am using yii extension ECSVExport to generate csv.

I want an output like this:

ID  Value
1   A
    B
2   C
3   E
    FF
    G
4   X
Concuss answered 3/7, 2013 at 12:2 Comment(1)
Have you tried with the define PHP_EOL ?Togetherness
V
26

Warning: this short remark is not a generic answer to the question. Consider checking another answer below.

Try this

"\n"

inside double quote

Vulture answered 3/7, 2013 at 12:8 Comment(3)
No. Result is Finland : 03/07/2013"\n"Ireland : 03/07/2013"\n"Denmark : 03/07/2013Concuss
He meant double quotes around the whole string, instead of simple quotes.Togetherness
in case anyone looking for getting newline using implode with array of string. use implode(PHP_EOL) will do the trick.Absurd
P
25

Use "\n" inside the cell value (wrapped in ") and "\r\n" for your end-of-record marker. If your cell entry include multiple lines, then you must enclose it in "

$fh = fopen('test1.csv', 'w+');
fwrite($fh, "sep=\t" . "\r\n");
fwrite($fh, 'A' ."\t" . 'B' . "\t" . 'C' . "\r\n");
fwrite($fh, 'D' ."\t" . "\"E\nF\nG\"" . "\t" . 'H' . "\r\n");
fclose($fh);

or (working with variables)

$varA = 'A';
$varB = 'B';
$varC = 'C';
$varD = 'D';
$varE = 'E';
$varF = 'F';
$varG = 'G';
$varH = 'H';
$fh = fopen('test2.csv', 'w+');
fwrite($fh, "sep=\t"."\r\n");
fwrite($fh, $varA . "\t" . $varB . "\t" . $varC . "\r\n");
fwrite($fh, $varD . "\t" . "\"$varE\n$varF\n$varG\"" . "\t" . $varH . "\r\n");
fclose($fh);

or (using fputcsv())

$fh = fopen('test3.csv', 'w+');
fwrite($fh, "sep=\t" . "\r\n");
fputcsv($fh, array('A', 'B', 'C'), "\t");
fputcsv($fh, array('D', "E\nF\nG", 'H'), "\t");
fclose($fh);

or (using fputcsv() and working with variables)

$varA = 'A';
$varB = 'B';
$varC = 'C';
$varD = 'D';
$varE = 'E';
$varF = 'F';
$varG = 'G';
$varH = 'H';
$fh = fopen('test4.csv', 'w+');
fwrite($fh, "sep=\t" . "\r\n");
fputcsv($fh, array($varA, $varB, $varC), "\t");
fputcsv($fh, array($varD, "$varE\n$varF\n$varG", $varH), "\t");
fclose($fh);
Pops answered 3/7, 2013 at 12:16 Comment(7)
I believe this is for an end of line and not the line return required "inside a cell itself" in ExcelVinylidene
@Laurence - have you actually tried to see if it works or not before downvoting?Pops
Yes I have the same issue, tried all sorts. "\n" adds a new line at the end of the line for me (so starts a new row in excel) not a new line within the cell values itself. So we don't want a row but an in-cell line break when opening the csv in excel or numbers. Maybe it's not working as I'm on a Mac and using Numbers.Vinylidene
On closer inspection of your code you state this "\"E\nF\nG\"", so escape slashes before the enclosing ". How would this work with variables, e.g. $value1 is your E, $value2 your F etc? Can you expand/edit your answer to be more clear because it first states to just use "\n" which wont work, as this on its own puts a new line. Maybe this solution can work then, I am unsure how to represent your answer with the variables. e.g. would this work? "\"$value1\n$value2\n$value3\"" and if so could the first line be changed to be clear on this as its not clear.Vinylidene
Right, this works for me $cellvalue = '"'.$value1."\n".$value2."\n".$value3.'"'; Note the enclosing '"' around the whole thing. Could you update your post to reflect this? Basically "\n" DOES work on its own, BUT only when enclosing the whole thing in extra " also. Now I know this, I think thats what you meant, but obviously it was not clear to me, it needs this code example using variables (not plain text like you have). This was the bit that was not clear to me and why I could not get it to work. Then I can vote it up!Vinylidene
I've extended my example to try and show this using variablesPops
Thanks, I've upvoted it as I can understand it better now and works for me!Vinylidene

© 2022 - 2024 — McMap. All rights reserved.