Use PHP to download xls files (do-it-yourself) _php tips

Source: Internet
Author: User
Tags readfile
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!

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.