开发者

Exporting an array to CSV [closed]

开发者 https://www.devze.com 2023-04-09 05:30 出处:网络
Closed. This question is off-topic. It is not currently accepting answers. 开发者_C百科 Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed. This question is off-topic. It is not currently accepting answers.
开发者_C百科

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 10 years ago.

Improve this question

I 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号