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 for a view table in the properties dialog on the "Options" tab. In the lower part of the dialog, set the "Show file export" setting and then click on "Edit" to the right of it.

Select "Export" in the left-hand area of the dialog. Then click on "Add template" and select "Custom template".

Click on "Edit template".

You can now write your own VM file to control the export. Open the Velocity Editor and write 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 in Velocity should be terminated with ##.

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. It must be noted separately whether this is a long text field. 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.