Advanced Techniques - 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.
Click on
"Add template" here
and select "User-defined template". Afterwards, click on
"Edit template".
Now, a custom VM file can be written that controls the export.
The example file "exportExample.vm" is available in the
portal directory internal/system/vm/export.
Open the Velocity editor and define the following script:
## Retrieve table data:
#set($data = $ExportUtil.getExport($RenderingContext, $ProcessingContext, $l_strExportGuid))##
## Initialize HTML remove renderer
#set($GdDefaultHtmlRemoveRenderer = $RendererFactory.createHtmlRemoveRenderer(false, true, ""))##
##Write header for the 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)##
##Include column headers ?
#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##
-----------------------------------------------------------
My company data export from $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.