Activity Forums Salesforce® Discussions How can I create a new line in a Visualforce CSV without using Apex code?

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

    Posted by Deepak on October 11, 2019 at 12:57 pm

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

    Laveena replied 4 years, 6 months ago 2 Members · 1 Reply
  • 1 Reply
  • Laveena

    Member
    October 11, 2019 at 1:04 pm

    Hi Deepak,

    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,   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>

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos