Small example of php export (generate) CSV file

Source: Internet
Author: User
Small example of php export (generate) CSV file

  1. /**
  2. * Generate CSV files separated by commas by default.
  3. * Solution: the content contains commas (,) and double quotation marks ("")
  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=csvdata .csv ");
  9. $ Rs = array (
  10. Array ('A', "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;

Note: You must pay attention to the encoding when exporting csv files in php. >>> For more information about exporting csv files from php.

Related Article

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.