Php exports csv data (the output in the browser provides download or save to a file)

Source: Internet
Author: User
Php exports csv data (the output in the browser provides download or save to a file)

  1. /**
  2. * Export data to a CSV file
  3. * @ Param array $ 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 ("maid", "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]." \ "\ r \ n ";
  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]." \ "\ r \ n ";
  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") {// Fix the Chinese name garbled by IE browser
  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. export csv data in php and save it to a file.

  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]." \ "\ r \ n ";
  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]." \ "\ r \ n ";
  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. }

Appendix: example of calling php to export csv data (saved to a file ):

  1. $ File_name = "/var/www/tmp/test.csv ";
  2. $ Header = array (
  3. '0' => 'parameter ID ',
  4. '1' => 'parameter name ',
  5. '2' => 'statistical times ',
  6. '3' => 'Count percentage ',
  7. '4' => 'unique user size ',
  8. '5' => 'percentage of unique users ',
  9. '6' => 'average number'
  10. );
  11. $ CsvList = array ("111", "title", "12", "100%", "23", "50%", "4 "));
  12. Export_csv ($ csvList, $ header, $ file_name );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.