Find your content:

Search form

You are here

How can I create a new line in a Visualforce CSV without using Apex code?

 
Share

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/>, &#010;,&#012;,&#013;, also with each using escape="false" but nothing seems to work 100% correctly. Tried outputting all of the above in the value of the outputText and also on their own (i.e., not in an apex: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.


Attribution to: Peter Knolle

Possible Suggestion/Solution #1

The solution for me was not to use <apex:outputText>, try the below:

<apex:page 
 readOnly="true"
 contentType="application/octet-stream#MyCsv.csv"
 sidebar="false"
 standardStylesheets="false"
 showHeader="false"
 cache="true"
 expires="0"><!--  
-->Column 1,Column 2,Column 3
aVal1,aVal2,aVal3
bVal1,bVal2,bVal3<!--
--></apex:page>

Attribution to: Daniel Sokolowski

Possible Suggestion/Solution #2

With no other answers posted, I'm afraid this may be the best that you're able to achieve.

There doesn't seem to be a clean way of doing this. I think the best way, is to use Apex, but, after experimenting there may be one extremely hacky way to achieve this. Based on your example of \n at the beginning of the line forcing newLines, but also outputting the literal value, I tried a couple of things. First, &nbsp; at the beginning will force a newline, but also include a space in the first column, I couldn't find a way around this, so I just added a rowCount column, to force this newline.

<apex:page readOnly="true"
       contentType="application/octet-stream#MyCsv.csv"
       sidebar="false"
       standardStylesheets="false"
       showHeader="false"
       cache="true"
       expires="0">


<!--
This will create a newLine, but will insert an empty space in the first column of every row.

<apex:outputText value="Column 1,Column 2,Column 3"/>
&nbsp;<apex:outputText value="aVal1,aVal2,aVal3"/>
&nbsp;<apex:outputText value="bVal1,bVal2,bVal3"/>
&nbsp;<apex:outputText value="bVal1,bVal2,bVal3"/>
&nbsp;<apex:outputText value="bVal1,bVal2,bVal3"/>
&nbsp;<apex:outputText value="bVal1,bVal2,bVal3"/>
-->

<!--
Use a dummy "rowCount" column to still force a newline, but preserve the actual value (e.g., no uneccessary space)
and attempt to not lose too much value by having a somewhat not-useless column (debatable)
-->
<apex:variable value="{!1}" var="rowNum"/>
<apex:outputText value="Row,Column 1,Column 2,Column 3"/>
{!rowNum},<apex:outputText value="aVal1,aVal2,aVal3"/><apex:variable var="rowNum" value="{!rowNum+1}"/>
{!rowNum},<apex:outputText value="bVal1,bVal2,bVal3"/><apex:variable var="rowNum" value="{!rowNum+1}"/>
{!rowNum},<apex:outputText value="bVal1,bVal2,bVal3"/><apex:variable var="rowNum" value="{!rowNum+1}"/>
{!rowNum},<apex:outputText value="bVal1,bVal2,bVal3"/><apex:variable var="rowNum" value="{!rowNum+1}"/>
{!rowNum},<apex:outputText value="bVal1,bVal2,bVal3"/><apex:variable var="rowNum" value="{!rowNum+1}"/>
</apex:page>

Attribution to: Mikey

Possible Suggestion/Solution #3

You don't required to use apex only for new line in csv. If you Press enter after writing code for first row, 2nd row will come in new line automatically. Try code below as it is, you will understand.

<apex:page readOnly="true"
       contentType="application/octet-stream#MyCsv.csv"
       sidebar="false"
       standardStylesheets="false"
       showHeader="false"
       cache="true"
       expires="0">
<br/>value1ForRow1, value2ForRow1<br/>
value1ForRow2, value2ForRow2<br/>
</apex:page>

Attribution to: SFBlogForce

Possible Suggestion/Solution #4

<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 to: cwall
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4742

My Block Status

My Block Content