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. ='
| '. $ Item .' | ';}$ Table_data. ='
';}$ Table_data. ='
';
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