Use php to download the xls file (manually written ). When I checked the ECSHOP source code yesterday, I encountered something I had never learned but how to use php to download xls files. Based on its source code, I implemented it first, and successfully realized this. when I checked the ECSHOP source code yesterday, I encountered something I had never learned but 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;
}
$ FileName = 'test.xls ';
DownloadXls ($ fileName );
?>
Effect:
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 );
}
$ FileName = 'test.xls ';
DownloadXls ($ fileName );
?>
Effect:
Expand: What if I want to download a txt file? What about a PDF file?
The method is to modify the Content in Content-Type in the header output!
There are not many things, please point out!
Bytes. Based on its source code, I implemented it and successfully implemented this...