Home >Backend Development >PHP Tutorial >PHP generates excel files without using COM

PHP generates excel files without using COM

高洛峰
高洛峰Original
2016-12-02 11:39:51983browse

Use PHP (as the current mainstream development language) to generate excel files

<?
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
echo "test1";
echo "test2";
echo "test1";
echo "test2";
echo "test1";
echo "test2";
echo "test1";
echo "test2";
echo "test1";
echo "test2";
echo "test1";
echo "test2";
?>

Running the above code in the PHP (as the current mainstream development language) environment, you can see the browser asking the user whether to download the excel document , click save, and there will be an extra excel file on the hard disk. Open it in excel and you will see the final result. How good is it?
 In fact, when making real applications, you can take the data out of the database, and then echo it out by adding after each column of data and after each row of data, in PHP (as the current mainstream development language) Use header("Content-type:application/vnd.ms-excel"); at the beginning to indicate that the output is an excel file, and use header("Content-Disposition:filename=test.xls"); to indicate that the output file name is text .xls. That's OK.



We can also modify the header to allow it to output files in more formats, so that PHP (as the current mainstream development language) will be more convenient in processing various types of files.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn