Heim  >  Artikel  >  Backend-Entwicklung  >  php导出(生成)CSV文件的小例子

php导出(生成)CSV文件的小例子

WBOY
WBOYOriginal
2016-07-25 08:55:06865Durchsuche
  1. /**
  2. * 生成默认以逗号分隔的CSV文件
  3. * 解决:内容中包含逗号(,)、双引号("")
  4. * @author zf Edit: bbs.it-home.org
  5. * @version 2012-11-14
  6. */
  7. header("Content-Type: application/vnd.ms-excel; charset=GB2312");
  8. header("Content-Disposition: attachment;filename=CSV数据.csv ");
  9. $rs = array(
  10. array('aa', "I'm li lei", '"boy"', '¥122,300.00'),
  11. array('cc', 'I\'m han mei', '"gile"', '¥122,500.00'),
  12. );
  13. $str = '';
  14. foreach ($rs as $row) {
  15. $str_arr = array();
  16. foreach ($row as $column) {
  17. $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
  18. }
  19. $str.=implode(',', $str_arr) . PHP_EOL;
  20. }
  21. echo $str;
复制代码

说明:有时务必注意php导出csv文件时的编码问题。 >>> 更多有关php 导出csv的内容。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn