Normally, I’d be iterating over a collection of records, but to simplify it I just have a header and two rows hard-coded, so that I could easily test it.
<apex:page readOnly="true" contentType="application/octet-stream#MyCsv.csv" sidebar="false" standardStylesheets="false" showHeader="false" cache="true" expires="0"> <apex:outputText value="Column 1,Column 2,Column 3"/> <!-- what to put here ?--> <apex:outputText value="aVal1,aVal2,aVal3"/> <!-- ...and here? --> <apex:outputText value="bVal1,bVal2,bVal3"/> </apex:page>
It works if I use a controller property and reference it as follows, but I’d rather not put that new line in the controller.
public String getNewLine() { return '\n'; } <apex:outputText value="{!newLine}"/>
I’ve tried
apex:outputText value="\n"
,\r
,\r\n
,BR()
,<br/>
,

,
,
, also with each usingescape="false"
but nothing seems to work 100% correctly. Tried outputting all of the above in thevalue
of the outputText and also on their own (i.e., not in anapex:outputText
). Still couldn’t work.One thing that I did notice was that if I put the \n at the beginning of the line it does create a new line in the CSV, but literal \n also appears in the file:
\n<apex:outputText value="aVal1,aVal2,aVal3"/>
does output on a new line, but the output includes the literal ‘\n’:
\naVal1,aVal2,aVal3
If it matters, using Windows and Excel to open the CSV.
Answer
<apex:variable value="" var="newline"/>
<apex:outputText value="Column 1,Column 2,Column 3"/> <!-- what to put here ?-->
{!newline}<apex:outputText value="aVal1,aVal2,aVal3"/> <!-- ...and here? -->
{!newline}<apex:outputText value="bVal1,bVal2,bVal3"/>
Attribution
Source : Link , Question Author : Peter Knolle , Answer Author : cwall