0
votes

The input to my freemarker template is records: List<List<String>> records = new ArrayList(); each list inside records is a row from a database; I need to create a csv; How I can write values of a row in a separate line to create a csv.

Here is my freemarker template: for now it write any value in a different line.

   <#list records>
    <#items as record>
        <#list record>
        <#items as value>
           ${value},
        </#items>
        </#list>
    </#items>
    </#list>

``````````
    
1

1 Answers

0
votes

In simple cases #items is not needed, so I give the example without that (but same approach works with #items as well).

<#list record as value>${value}<#sep>,</#list>

That is, #list (and #items) just repeatedly prints the content nested between its start tag and end tag (except, it ignores the line-break directly after the start tag). So if you don't have a line-break at the end of the nested content, then it doesn't print a line-break.

As of #sep, I added that because you don't want comma after the last value of the record.