Exporting an array to CSV [closed]
开发者_C百科
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this questionI need a module for Drupal 7 that can export CSV when given an array of values.
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.
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
精彩评论