Export reports and import reports using PHP's own functions Fputcsv and fgetcsv

Source: Internet
Author: User
PHP's own function fputcsv can be used to print the report (Excel) function. If your report format requirements are not very high, then fputcsv is a good choice. It performs efficiently and does not require a third-party library, which is convenient to use.

 
  

The above code will generate a CSV file locally, can be opened in Excel, is not very simple. If there is Chinese, after Linux execution, download to the local open will be garbled, then you can use the Iconv function to convert.

$list = Array ();

$tmp = "Order number, order payment amount, lucky number, user name, user type, period, number generation time, draw time, award logo, Prize, bonus, notes";

$list [] = Iconv (' UTF-8 ', ' Gb2312//ignore ', $tmp);

Export the generated CSV directly to the browser

Header (' content-disposition:attachment; filename=contacts.csv ');//If the file name is in Chinese, the UrlEncode will not appear in IE after the Chinese characters.

Header (' Content-type:application/octet-stream ');

Header (' Content-length: '. FileSize (' contacts.csv '));//File size

ReadFile ($file _path);

Exit ();

Second, import the report with Fgetcsv

There is a point to be aware of when importing reports with Fgetcsv. It is necessary to convert the EXCEL document into CSV format. Note: It is not easy to change the suffix name.

 
  
  • 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.