A CSV referred to Comma separated values. This is widely supported format for transferring data between applications. Data can be exported with a .CSV file and it also can be imported by uploading .CSV file. However, in this blog we’ll see the implementation of CSV exports using php. Implementing a code for exporting data to CSV is useful in many applications. We have written this code in our product JGive to export donor data.

Note: This blog is purely targeted to developers!

 

Sample Image

Here goes the code:

<?php

$csvExport = null;

// Column name separated by using semicolon(;) as a delimiter 
$csvExport .= 'Id;Donor Name;Campaign Name;Donation amount';
$csvExport .= "\n";

// Code to write and download xsl file using php
header("Content-type: application/vnd.ms-excel");

// Set file name
$fileName = 'student-report'.date("Y-m-d");
header("Content-disposition: filename=" . $fileName . ".csv");

// Append 1st record, each column value separated by semicolon 
$csvExport .= '1;David;Go Green Save Earth;$100';

// Add new line after each record
$csvExport .= "\n";

// Append 2nd record
$csvExport .= '2;Josh;Health and Humanity;$50';

// Discards the contents of the output buffer.
ob_clean();

// Echo the csv data
echo $csvExport."\n";
exit();

?>

 

 I hope this will help you implement it in your products!