开发者

Exporting an array to CSV [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜