How to use php to download xls files. According to the source code of ECSHOP, I implemented it myself. the following examples will be used to share with you the results of viewing the source code of ECSHOP yesterday, what I have never learned is how to use php to download xls files. Based on its source code, I implemented it and successfully achieved this effect.
Source code:
The code is as follows:
/*
* @ Description: Download the xls table
*
*
*/
Function downloadXls ($ filename = ''){
$ Filename =! Empty ($ filename )? $ Filename: die ('Nothing ');
// The header is used to create a new test.xls file.
Header ("Content-Type: application/vnd. ms-excel; charset = utf8 ");
Header ("Content-Disposition: attachment; filename = $ filename ");
// The output content is directly exported to the test.xls file.
Echo 'This is the test! ';
Exit;
}
Note: If Chinese characters are output, note the format conversion of character encoding!
But what if I want to download the xls files saved on the server?
After reading the php manual, I found that this function can be implemented very easily. I used a readfile function. The code is as follows:
The code is as follows:
/*
* @ Description: Download the xls table
*
*
*/
Function downloadXls ($ filename = ''){
$ Filename =! Empty ($ filename )? $ Filename: die ('Nothing ');
// The header is used to create a new test.xls file.
Header ("Content-Type: application/vnd. ms-excel; charset = utf8 ");
Header ("Content-Disposition: attachment; filename = $ filename ");
// Here is the file to be output
Readfile ($ filename );
}
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.