export sql table to excel php phpexcel code example
Example: export html table data to excel in php
Use a PHP Excel for generatingExcel file. You can find a good one called PHPExcel here: https://github.com/PHPOffice/PHPExcel
And for PDF generation use http://princexml.com/