Adding a newline character within a cell (CSV)

ExcelCsv

Excel Problem Overview


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?

Excel Solutions


Solution 1 - Excel

This question was answered well at https://stackoverflow.com/questions/566052/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.

Solution 2 - Excel

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"; 

Solution 3 - Excel

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.

Solution 4 - Excel

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);	

Solution 5 - Excel

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

Solution 6 - Excel

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 + ' "' 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2997761View Question on Stackoverflow
Solution 1 - Excelrajah9View Answer on Stackoverflow
Solution 2 - ExcelDan RobidouxView Answer on Stackoverflow
Solution 3 - ExcelLuan NguyenView Answer on Stackoverflow
Solution 4 - ExcelkantsvermaView Answer on Stackoverflow
Solution 5 - ExcelSamView Answer on Stackoverflow
Solution 6 - ExcelJonathanView Answer on Stackoverflow