Tips & Tricks - Table export - User-defined template
This workshop provides a guide to creating your own templates for data that is exported from tables in applications.
The export function can be activated on the "Options" tab in the properties dialog of a view table. Activate the "Display file export" setting and then click on "Edit" to the right of this.
Select "Export" in the left area. Click on "Add template" there and select "User-defined template".
Afterwards, click on "Edit template".
Now, a custom VM file can be written that controls the export. Open the Velocity editor and enter the following script.
## Get table data:
#set($data = $ExportUtil.getExport($RenderingContext, $ProcessingContext, $l_strExportGuid))##
## HTML remove renderer initialisieren
#set($GdDefaultHtmlRemoveRenderer = $RendererFactory.createHtmlRemoveRenderer(false, true, ""))##
##Write header for output file
$Response.setHeader("Content-Type","text/plain; charset=UTF-8")$Response.setHeader("Accept-Ranges","bytes")$Response.setHeader("Content-Disposition","attachment;filename=${l_strOutputFilename}")##
$Response.setIgnoreWrite(false)##
##Get column header ?
#if($bShowHeader == true)##
#foreach($header in $data.getColumnInfos())##
#if($velocityCount>1)${SepChar}#end##
"$header.getTitle()"##
#end##
$ESC.getLF()##
#end##
##Iterate over all rows and columns and output values
#foreach($row in $data)##
#foreach($column in $row)##
#if($velocityCount>1)${SepChar}#end##
#if($l_bRemoveHtml && $column.getColumnInfo().getType()=="text" && $column.isStringType())##
"$GdDefaultHtmlRemoveRenderer.getOutput($column.asValueHolder()).replaceAll('"','""')"##
#else##
"$column.asText().replaceAll('"','""')"##
#end##
#end##
$ESC.getLF()##
#end##
-----------------------------------------------------------
Sample company data export on $DtUtil.now($User.getTimeZone())
-----------------------------------------------------------
$Response.setIgnoreWrite(true)
This example exports the available data in a text file where each of the column values are given the double quotation marks as a separator.
Line breaks should be generated with "$ESC.getLF()". To avoid incorrect breaks, lines should be closed with ## in Velocity.
With
$header.getTitle()
the column title will be written as the first line in the output file. After this section for the column headers, the code iterates over every row ($row) of the table and over every column ($column) within this row to write the table values in the export. If the table contains long text fields, Any quotation marks in the text will be masked because they are used as separators, as mentioned above. At the end of the file, a footer with the current date is defined to record the time of the export. Save the application and test the result in the browser.