76

I would like to import product descriptions that need to be logically broken according by things like description, dimensions, finishes etc. How can I insert a line break so that when I import the file they will show up?

7 Answers7

50

I struggled with this as well but heres the solution. If you add " before and at the end of the csv string you are trying to display, it will consolidate them into 1 cell while honoring new line.

csvString += "\""+"Date Generated: \n" ; 
csvString += "Doctor: " + "\n"+"\"" + "\n"; 
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
Dan Robidoux
  • 509
  • 4
  • 2
49

This question was answered well at Can you encode CR/LF in into CSV files?.

Consider also reverse engineering multiple lines in Excel. To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a .csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.

Community
  • 1
  • 1
rajah9
  • 11,645
  • 5
  • 44
  • 57
  • 11
    Unfortunately, this file cannot be imported correctly by Excel. Excel will import them as multiple cells (at least, for Mac version). – Tom Yeh Feb 04 '20 at 08:23
  • The Mac uses a different combination to enter a line break. This site: https://apple.stackexchange.com/questions/84609/how-to-add-a-line-break-in-a-cell-in-excel-for-mac says "The answer is to use Control+Option+Enter, which will create a line break in the cell." – rajah9 Feb 04 '20 at 13:27
  • 5
    Sorry, I probably didn't make it clear. I didn't mean how to enter a line break in Excel. I mean if you export a CSV with a cell containing line break and then import the file back, Excel cannot handle it correctly. Here is [some workaround](https://superuser.com/questions/319549/importing-multiline-cells-from-csv-file-into-excel/1494059#1494059) – Tom Yeh Feb 05 '20 at 02:08
2

On Excel for Mac 2011, the newline had to be a \r instead of an \n

So

"\"first line\rsecond line\""

would show up as a cell with 2 lines

Sam
  • 5,375
  • 2
  • 45
  • 54
2

I have the same issue, when I try to export the content of email to csv and still keep it break line when importing to excel.

I export the conent as this: ="Line 1"&CHAR(10)&"Line 2"

When I import it to excel(google), excel understand it as string. It still not break new line.

We need to trigger excel to treat it as formula by: Format -> Number | Scientific.

This is not the good way but it resolve my issue.

Luan Nguyen
  • 186
  • 1
  • 5
2

supposing you have a text variable containing:

const text = 'wonderful text with \n newline'

the newline in the csv file is correctly interpreted having enclosed the string with double quotes and spaces

'" ' + text + ' "' 
Jonathan
  • 595
  • 5
  • 10
0

I was concatenating the variable and adding multiple items in same row. so below code work for me. "\n" new line code is mandatory to add first and last of each line if you will add it on last only it will append last 1-2 character to new lines.

  $itemCode =  '';
foreach($returnData['repairdetail'] as $checkkey=>$repairDetailData){

    if($checkkey >0){
        $itemCode   .= "\n".trim(@$repairDetailData['ItemMaster']->Item_Code)."\n";
    }else{
        $itemCode   .= "\n".trim(@$repairDetailData['ItemMaster']->Item_Code)."\n";             
    }
    $repairDetaile[]= array(
        $itemCode,
    )
}
// pass all array to here 
foreach ($repairDetaile as $csvData) { 
    fputcsv($csv_file,$csvData,',','"');
}
fclose($csv_file);  
kantsverma
  • 606
  • 1
  • 11
  • 31
0

I converted a pandas DataFrame to a csv string using DataFrame.to_csv() and then I looked at the results. It included \r\n as the end of line character(s). I suggest inserting these into your csv string as your row separation.

Depending on the tools used to generate the csv string you may need escape the \ character (\r\n).

MustardMan
  • 43
  • 1
  • 7