Php to generate an excel file (csv) simple code _ PHP Tutorial

Source: Internet
Author: User
Php generates simple code for an excel file (csv. CSV is the abbreviation of comma-separated values. it is usually a plain text file. We recommend that you use WORDPAD or notepad (NOTE) to enable it. then, save a new file and use EXCEL to enable it. you can also use CSV to separate values with commas, it is usually a plain text file. We recommend that you use WORDPAD or notepad (NOTE) to enable it. then, save a new file and enable it in EXCEL. you can also open it in excel, just like in excel.

The php reference code for generating an excel file (csv) is as follows:

The code is as follows:
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment; filename = www. bKjia. c0m test_data.xls ");
// The output content is as follows:
Echo "name". "t ";
Echo "age". "t ";
Echo "education". "t ";
Echo "n ";
Echo "James". "t ";
Echo "25". "t ";
Echo "www.111Cn.net section". "t ";
?>

The following method is extracted from a project (only 1997-2003 supported) t is a Tab

The code is as follows:

/**
* Export an Excel table
*
*/
Public function excel (){
// Indicates that an excel file is output.
Header ("Content-type: application/vnd. ms-excel ;");
// The output file name is lamp_dtype.xls.
Header ("Content-Disposition: filename=lamp_dtype.xls ");
$ Exc. = "type name t dormitory category t dormitory area t number of people t monthly rent/PERSON t remarks tn ";
$ Dtype = D ('dtype ');
// Query information in the database
$ Datas = $ dtype-> select ();
// Output each piece of information cyclically
Foreach ($ datas as $ data ){
$ Exc. = "{$ data ['name']} t {$ data ['type']} t {$ data ['region']} t {$ data ['Capacity ']} t {$ data ['Rent ']} t {$ data ['remark']} tn ";
}
// UTF-8 to GB2312
$ Exc = iconv ("UTF-8", "GB2312", $ exc );
Echo $ exc;

// Avoid outputting the following content

Exit ();
}
?>

========================================================== ==========

Generate csv: comma is Tab

The abbreviation of delimiter (comma-separated values), which is usually a plain text file. We recommend that you use WORDPAD or notepad (NOTE) to enable it, and then save a new file and use EXCEL to enable it...

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.