When I was looking at the ECSHOP source code yesterday, I came across something I had never learned before - how to use php to download xls files. Based on its source code, I implemented it and successfully achieved this effect.
Source code:
Copy code The code is as follows:
/*
*@Description: Download xls table
*
*
*/
function downloadXls($filename=''){
$filename = !empty($filename) ? $filename : die('nothing');
//The function of header is to create a new downloaded test.xls
header("Content-Type: application/vnd.ms-excel; charset=utf8" );
header("Content-Disposition: attachment; filename=$filename");
//The content that needs to be output here is directly output 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 an xls file saved in the server?
After checking the php manual: I found that this function can be implemented very easily, using a readfile function. The code is as follows:
Copy code The code is as follows:
/*
*@Description :Download xls table
*
*
*/
function downloadXls($filename=''){
$filename = !empty($filename) ? $filename : die('nothing ');
//The function of header is to create a new downloaded test.xls
header("Content-Type: application/vnd.ms-excel; charset=utf8");
header("Content-Disposition: attachment; filename=$filename");
//Here is the file that needs to be output
readfile($filename);
}
$fileName = 'test.xls';
downloadXls($fileName);
?>
Effect:
Let’s expand a little further: What if I want to download a txt file, what about a pdf file?
The way to achieve this is to modify the content in Content-Type in the header output!
If there is anything missing, please point it out!
http://www.bkjia.com/PHPjc/756338.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/756338.htmlTechArticleWhen I was looking at the ECSHOP source code yesterday, I came across something I had never learned before - how to use php to download xls document. According to its source code, I implemented it and successfully achieved this...