Home > Article > Backend Development > PHP generates excel files without using COM
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.