Home  >  Article  >  Backend Development  >  PHP exports csv data (the output in the browser can be downloaded or saved to a file)

PHP exports csv data (the output in the browser can be downloaded or saved to a file)

WBOY
WBOYOriginal
2016-07-25 08:53:201143browse
  1. /**
  2. * Export data to CSV file
  3. * @param array $data data
  4. * @param array $title_arr title
  5. * @param string $file_name CSV file name
  6. */
  7. function export_csv(&$data, $title_arr, $file_name = '') {
  8. ini_set("max_execution_time", "3600");
  9. $csv_data = '';
  10. /**title*/
  11. $nums = count($title_arr);
  12. for ($i = 0; $i < $nums - 1; ++$i) {
  13. $csv_data .= '"' . $title_arr[$i] . '",';
  14. }
  15. if ($nums > 0) {
  16. $csv_data .= '"' . $title_arr[$nums - 1] . ""rn";
  17. }
  18. foreach ($data as $k => $row) {
  19. for ($i = 0; $i < $nums - 1; ++$i) {
  20. $row[$i] = str_replace(""", """", $row[$i]);
  21. $csv_data .= '"' . $row[$i] . '",';
  22. }
  23. $csv_data .= '"' . $row[$nums - 1] . ""rn";
  24. unset($data[$k]);
  25. }
  26. $csv_data = mb_convert_encoding($csv_data, "cp936", "UTF-8");
  27. $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;
  28. if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")) { // 解决IE浏览器输出中文名乱码的bug
  29. $file_name = urlencode($file_name);
  30. $file_name = str_replace('+', '%20', $file_name);
  31. }
  32. $file_name = $file_name . '.csv';
  33. header("Content-type:text/csv;");
  34. header("Content-Disposition:attachment;filename=" . $file_name);
  35. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  36. header('Expires:0');
  37. header('Pragma:public');
  38. echo $csv_data;
  39. }
复制代码

2,php导出csv数据,并保存到文件

  1. function export_csv($data, $title_arr, $file_name = '') {

  2. $csv_data = '';

  3. /**title*/

  4. $nums = count($title_arr);
  5. for ($i = 0; $i < $nums - 1; ++$i) {
  6. $csv_data .= '"' . $title_arr[$i] . '",';
  7. }

  8. if ($nums > 0) {

  9. $csv_data .= '"' . $title_arr[$nums - 1] . ""rn";
  10. }

  11. foreach ($data as $k => $row) {

  12. for ($i = 0; $i < $nums - 1; ++$i) {
  13. $row[$i] = str_replace(""", """", $row[$i]);
  14. $csv_data .= '"' . $row[$i] . '",';
  15. }
  16. $csv_data .= '"' . $row[$nums - 1] . ""rn";
  17. unset($data[$k]);
  18. }

  19. $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;

  20. file_put_contents($file_name, $csv_data) ;
  21. }

复制代码

附,php导出csv数据的调用示例(保存到文件):

  1. $file_name="/var/www/tmp/test.csv" ;
  2. $header = array(
  3. '0' => '参数ID',
  4. '1' => '参数名称',
  5. '2' => '统计次数',
  6. '3' => '统计次数百分比',
  7. '4' => '唯一用户数',
  8. '5' => '唯一用户数百分比',
  9. '6' => '人均次数'
  10. );
  11. $csvList = array(array("111", "title", "12", "100%", "23", "50%", "4")) ;
  12. export_csv($csvList, $header, $file_name) ;
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn