How to export and generate an excel file using php-PHP source code

Source: Internet
Author: User
Using php to export excel files, most of us will generate csv files directly. This is convenient and quick. If it is not highly required, we can use csv files for instances. Using php to export excel files, most of us will generate csv files directly, this is convenient and quick. If it is not highly demanding, you can use csv to take the instance,

Script ec (2); script

This is the simplest.

The Code is as follows:

Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=test_data.xls ");

$ Tx = 'header ';
Echo $ tx. "nn ";
// The output content is as follows:
Echo "name". "t ";
Echo "Age". "t ";
Echo "education". "t ";
Echo "n ";
Echo "James". "t ";
Echo "25". "t ";
Echo "Undergraduate". "t ";
?>

If you must enter the xls standard excel file, refer to the following method:

/**
* Output the XLS header information
* Note: No data output should be performed before and after the function is used.
* @ Param $ data Array the downloaded data
* @ Param $ file_name String the downloaded file name
*/

The Code is as follows:

Function outputXlsHeader ($ data, $ file_name = 'Port ')
{
Header ('content-Type: text/xls ');
Header ("Content-type: application/vnd. ms-excel; charset = UTF-8 ");
$ Str = mb_convert_encoding ($ file_name, 'gbk', 'utf-8 ');
Header ('content-Disposition: attachment; filename = "'. $ str. '.xls "');
Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 ');
Header ('expires: 0 ');
Header ('pragma: public ');

$ Table_data ='

Foreach ($ data as $ line)
{
$ Table_data. ='









'; ';Foreach ($ line as $ key =>&$ item){$ Item = mb_convert_encoding ($ item, 'gbk', 'utf-8 ');$ Table_data. =' ';}$ Table_data. =' ';}$ Table_data. ='
'. $ Item .'
';
Echo $ table_data;
Die ();
}

The third-party practices are also recommended below

Reference the small library recommended by google code (roughly same as method 1, more complex)

Http://code.google.com/p/php-excel/downloads/list

PHPEXCEL class library, powerful, supports win Excel2003, Win Excel2007

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.