PHP Export CSV data (in-browser output available for download or save to file)

Source: Internet
Author: User
  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]. "\" \ 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 bug in IE output Chinese name garbled
  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. }
Copy Code

2,php export CSV data and save to file

    1. !--? php function export_csv ($data, $title _arr, $file _name = ") {

    2. li>

      $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. }

Copy Code

Attached, PHP export CSV data invocation example (save to file):

    1. $file _name= "/var/www/tmp/test.csv";
    2. $header = Array (
    3. ' 0 ' = ' parameter id ',
    4. ' 1 ' = ' parameter name ',
    5. ' 2 ' = ' statistics count ',
    6. ' 3 ' = ' Percentage of statistics ',
    7. ' 4 ' = ' Unique users ',
    8. ' 5 ' = ' percent of unique users ',
    9. ' 6 ' = ' per capita number '
    10. );
    11. $csvList = Array (Array ("111", "title", "12", "100%", "23", "50%", "4"));
    12. Export_csv ($csvList, $header, $file _name);
Copy Code
  • 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.