Tips & Tricks - Exporting Tables - Custom Template
In this post, you'll find instructions on how to create your own templates for data exported from tables in applications.
The export function can be enabled in the Properties dialog box for a view table on the "Options" tab. In the lower part of the dialog box, select the "Show file export" option, and then click "Edit" next to "
."
In the left pane of the dialog, select "Export." Then click "Add Template" under "
" and select "Custom Template."
Click "Edit Template" at
.
Now you can create 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 existing data to a text file, with double quotation marks used as delimiters for the individual column values.
Line breaks should be created using $ESC.getLF(). To avoid unintended line breaks, lines in Velocity should end with ##.
With
$header.getTitle()
Here, the column title is written as the first line in the output file. After the section for the column header, the program iterates through each row ($row) of the table and, within each row, through each column ($column) to write the table values to the export file. In this context, special attention must be paid to whether the field is a long text field. In any case, however, quotation marks that appear in the text are masked because, as mentioned above, the quotation mark is used as a field delimiter. A footer with the current date was added to the end of the file to record the time of the export. Now save the application and test the result in your browser.



