11

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.

7
  • Have you tried \r\n at the end of each line? Nov 23, 2012 at 20:47
  • Yes, I did. Updated the question. Nov 23, 2012 at 20:50
  • Wow, very odd. I can't seem to find a clean way of doing this, at all. 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. &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. Example: gist.github.com/4137588
    – Mikey
    Nov 23, 2012 at 22:34
  • @Mikey you should post that as an answer, it will get my upvote Nov 25, 2012 at 21:10

4 Answers 4

9
<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"/>
1
  • 3
    Works, but why does it work!?
    – metadaddy
    Nov 27, 2012 at 19:21
3

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>
2
  • Have you found a solution to this? I'm thinking it might be necessary to use XSLT.
    – tggagne
    Jun 19, 2014 at 20:58
  • The best I found was adding a '\n' to the beginning of the first column of each row.
    – tggagne
    Jun 19, 2014 at 21:08
0

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>
3
  • True, but not what the question is asking. Issue is that if value1ForRow1 is outputted from a apex tag, it doesn't register as a new line as you'd expect in this case (since in most cases you wouldn't expect a tag being on a separate line implies a new line). Nov 26, 2012 at 14:03
  • Ok! Now try to put '"\n"' before apex:outputText. public String getNewLine() { return '"\n"'; } <apex:outputText value="{!newLine}"/> Nov 27, 2012 at 12:46
  • The newLine method worked for me. Also have to reduce all of the white space in the VF page, all of it, in order to get rid of the unnecessary carriage returns at the first row, etc. I have also used a tableExporter script in the past but for this client was trying to keep it simple: github.com/hhurz/tableExport.jquery.plugin
    – Eric Clay
    Jan 30, 2019 at 18:45
0

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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .