Yesterday to see Ecshop source code, encountered a little I did not learn just--how to use PHP implementation download xls files. According to its source code, I started to achieve a bit, successfully achieved this effect.
Source:
Copy Code code as follows:
<?php
/*
* @Description: Download xls table
*
*
*/
function Downloadxls ($filename = ' ") {
$filename =!empty ($filename)? $filename: Die (' nothing ');
The header function is to create a new downloaded Test.xls
Header ("Content-type:application/vnd.ms-excel; Charset=utf8 ");
Header ("content-disposition:attachment; Filename= $filename ");
This needs to be output directly to the Test.xls file.
Echo ' This is the test! ';
Exit
}
$fileName = ' Test.xls ';
Downloadxls ($fileName);
?>
Effect:
Note: If the output is Chinese information, pay attention to the format conversion of character encoding!
But what if I want to download the XLS file saved in the server?
After viewing the PHP manual: found very simple to achieve this function, the use of a ReadFile function. The code is as follows:
Copy Code code as follows:
<?php
/*
* @Description: Download xls table
*
*
*/
function Downloadxls ($filename = ' ") {
$filename =!empty ($filename)? $filename: Die (' nothing ');
The header function is to create a new downloaded Test.xls
Header ("Content-type:application/vnd.ms-excel; Charset=utf8 ");
Header ("content-disposition:attachment; Filename= $filename ");
This is the file that needs to be exported.
ReadFile ($filename);
}
$fileName = ' Test.xls ';
Downloadxls ($fileName);
?>
Effect:
Expand again: If I want to download is a TXT file, pdf file?
The way to do this is to modify the contents of the Content-type in the header output!
There is not much place to ask the great God to point out!