Sunday, April 11, 2010

Tips and tricks: Exporting to CSV using Data Dynamics Reports

General
This is the first in a series of posts that will highlight small and simple but useful undocumented features of Data Dynamics Reports and ActiveReports. I have several topics in mind and hope that more ideas about such features come soon. I will start with the answer to question we were recently asked - is that possible to export the report to CSV format using Data Dynamics Reports?

Tip: use Xml Rendering Extension to get CSV output.
That sounds weird, but it's really not. Let me explain. Data Dynamics Reports allows you to save the data that are shown by a report to XML format. The output XML content does not include the information about report items style, pagination, interactivity, etc., it only includes the data that are grouped, sorted, filtered and displayed in the report's data regions. For example say that a report consists of a simple table that has two columns, the header and the details:

The report output in the viewer would look like below:

But the output that is produced by Xml Rendering Extension looks like:

<Report Name="Ages.rdlx">
<Table1>
<TextBox2>Age</TextBox2>
<TextBox1>Name</TextBox1>
<Detail_Collection>
<Detail>
<TextBox5>30</TextBox5>
<TextBox4>Jon</TextBox4>
</Detail>
<Detail>
<TextBox5>27</TextBox5>
<TextBox4>Michael</TextBox4>
</Detail>
<Detail>
<TextBox5>32</TextBox5>
<TextBox4>Bryan</TextBox4>
</Detail>
<Detail>
<TextBox5>29</TextBox5>
<TextBox4>Stewart</TextBox4>
</Detail>
</Detail_Collection>
</Table1>
</Report>

The element names in the output XML file can be fine-tuned using the properties on the individual report items, more details can be found on the corresponding help topic. That's all good, but how to get the same data in CSV format? For example:
Name,Age
Jon,30
Michael,27
Bryan,32
Stewart,29
Xml Rendering Extension has XsltStyleSheet setting, you can specify the stylesheet that will be applied to the output XML before producing the resulting document. Therefore you can write the XSLT that transforms XML content to CSV content. It's of course not possible to implement the uniform XSLT for all the reports, but the example for the table-based report where the table has the 1-row header and 1-row details is shown below. It works for any number of columns:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<!--Select the table header content -->
<xsl:for-each select="Report/Table1/*">
<xsl:if test="name(current())!='Detail_Collection'">
<xsl:if test="not(position() = 1)">,</xsl:if><xsl:value-of select="current()" />
</xsl:if>
</xsl:for-each>
<!--Insert the line break-->
<xsl:text>
</xsl:text>

<!--select the table details content -->
<xsl:for-each select="Report/Table1/Detail_Collection/Detail">
<xsl:for-each select="current()/*">
<xsl:if test="not(position() = 1)">,</xsl:if><xsl:value-of select="current()" />
</xsl:for-each>
<!--Insert the line break-->
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment