Use php to download xls files (manually written) _ PHP Tutorial

Source: Internet
Author: User
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...

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.