Datatable to XML CSV TAB HTML Json

I have a common functions class (cfc) project i tend to fill with all functions i believe can be applied to anything and will likely be reused.
I was making some adjustments to a few handy functions and I thought it might be useful to publish the following functions which will help convert datatables to various output formats.

Visual Studio references the following libraries within my cfc project, im not entirely sure which ones are required for these functions so i will list them all.

We have 6 functions here
1. ConvertDtToXML
2. ConvertDtToTDF
3. ConvertDtToCSV
4. ConvertDtToJSON
5. ConvertDtToHTML
6. RenderControl

Lets talk about each one briefly.
1. ConvertDtToXML
This function is making use of .NETs Dataset object built in method GetXml, the one simple rule here is that the table must be named in order for the xml to be generated. So we simply declare a dataset, check for a table name, if none then give a standard name, add the table to the dataset and output using GetXml.

2. ConvertDtToTDF
3 things are going on here, the first for loop we get the column headings. Second we begin a for loop on all of the rows, and third we for loop against each column getting the data this time. Each loop builds up the TAB delimited string we end up with.

3. ConvertDtToCSV
A very similar approach to the tab delimited has been taken here with the CSV, its the text used to build the sting that differs.

4. ConvertDtToJSON
We make use of .NETs Json serializer to convert our datatable to a json string

5. ConvertDtToHTML and 6. RenderControl
This final converter function ConvertDtToHTML needs to use last function RenderControl.
Load the datatable into a gridview, bind it, then extract the rendered HTML from it.

Leave a Reply

Your email address will not be published. Required fields are marked *