Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting an array to CSV [closed]

Tags:

csv

drupal-7

I need a module for Drupal 7 that can export CSV when given an array of values.


2 Answers

Sorry for the late response but I just ran across this while searching for my own solution. This is what I ended up with:

// send response headers to the browser
drupal_add_http_header('Content-Type', 'text/csv');
drupal_add_http_header('Content-Disposition', 'attachment;filename=csvfile.csv');

$fp = fopen('php://output', 'w');
foreach($csv_array as $line){
  fputcsv($fp, $line);
}
fclose($fp);
drupal_exit();

That should be in a page callback and it will result in a file download of a csv file with the array contents. Hope this helps.

like image 82
megensel Avatar answered Jul 23 '26 06:07

megensel


You don't need Drupal for this, you can use the PHP fputcsv function. Have a look at that page, there are clear examples of how to save an array as a CSV file.

Hope that helps

like image 35
Clive Avatar answered Jul 23 '26 07:07

Clive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!