Skip to main content
Inspiring
Jun 27, 2016 4:13 PM UTC
Answered

Report CSV file

I need to create a report using CFReport for CSV format.

I can create Excel, PDF, HTML, XML, RTF and Flash, but I am asked to produce CSV format.

I search CFReport, it does not have CSV option for the format.

I would like to know if it is possible to have CSV format using CFReport or ColdFusion have any other solution for this?

Your information and help is great appreciated,

Regards,

Sourises,

This topic is closed to new replies. Start a new post to keep the conversation going.
EddieLotter
EddieLotterCorrect answer
Inspiring
Jun 27, 2016 6:49 PM UTC

CSV is a data format, not a report format. You don't need cfreport to create a CSV file.

It's worth noting that since you know how to create an Excel file, the user can open the file in Excel and export it in CSV format.

Would you like to see an example of creating a CSV file without cfreport?

Cheers

Eddie

5 replies

EddieLotter
EddieLotterCorrect answer
Inspiring
Jun 27, 2016 6:49 PM UTC

CSV is a data format, not a report format. You don't need cfreport to create a CSV file.

It's worth noting that since you know how to create an Excel file, the user can open the file in Excel and export it in CSV format.

Would you like to see an example of creating a CSV file without cfreport?

Cheers

Eddie

Translate
iccsiAuthor
Inspiring
Jun 27, 2016 8:13 PM UTC

Thanks for the information and help,

Yes, if there is any example link to create a csv file would be appreciated,

Thanks again for helping,

Regards,

Sourises,

Translate
EddieLotter
Inspiring
Jun 27, 2016 9:11 PM UTC

The following is just a demonstration. You can use the concept to do what you want:

<cfsetting showdebugoutput="false">

<cfcontent type="text/csv" reset="true">

<cfheader name="Content-Disposition" value="attachment; filename=example.csv">

<cfscript>

  db = QueryNew("TextField1, NumericField, TextField2", "VarChar, Integer, VarChar");

  queryAddRow(db, [

    {TextField1="abc", NumericField=1, TextField2="def"},

    {TextField1='g"h"i', NumericField=2, TextField2="jkl"},

    {TextField1="mno", NumericField=3, TextField2="pqr"}

  ]);

  for (rowIndex = 1; rowIndex <= db.recordCount; rowIndex++) {

    writeOutput('"');

    writeOutput(replace(db.TextField1[rowIndex], '"', '""', 'all'));

    writeOutput('",');

    writeOutput(db.NumericField[rowIndex]);

    writeOutput(',"');

    writeOutput(replace(db.TextField2[rowIndex], '"', '""', 'all'));

    writeOutput('"');

  if ( rowIndex != db.recordCount )

    writeOutput(',' & chr(13) & chr(10));

}

</cfscript>

The above is the complete text in a cfm file. No HTML is required at all. When you access the file through your browser (for example http://localhost/demo.cfm) your browser will prompt you to save a file called "example.csv" which is a plain text file.

Cheers

Eddie

Translate