Skip to main content
Inspiring
June 27, 2016
Answered

Report CSV file

  • June 27, 2016
  • 1 reply
  • 1434 views

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 has been closed for replies.
Correct answer EddieLotter

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

1 reply

EddieLotter
EddieLotterCorrect answer
Inspiring
June 27, 2016

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

iccsiAuthor
Inspiring
June 27, 2016

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,

EddieLotter
Inspiring
June 27, 2016

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