This method uses Phpexcel to troubleshoot exporting Excel from a database
First, you need to download a phpexcel into the thinkphp directory under the three-party class library extend directory Vendor
Call Method Vendor (' PHPExcel.Classes.PHPExcel ');
Use a pub data to make a presentation.
The code is as follows:
1 Public functionExcel () {2 //Introducing the Phpexcel library file3Vendor (' PHPExcel.Classes.PHPExcel ');4 //Creating Objects5 $excel=NewPhpexcel ();6 //Excel table format, where 8 columns are briefly written7 $letter=Array(' A ', ' B ', ' C ', ' D ', ' E ', ' f ', ' f ', ' G ');8 //Table Header Array9 $tableheader=Array(' Serial number ', ' Inn name ', ' Inn address ', ' landline ', ' phone ', ' QQ ', ' email '));Ten //Populating header information One for($i= 0;$i<Count($tableheader);$i++) { A $excel->getactivesheet ()->setcellvalue ("$letter[$i]1 ","$tableheader[$i]"); - } - //Table Array the $data=Array( - Array(' 1 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), - Array(' 2 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), - Array(' 3 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), + Array(' 4 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), - Array(' 5 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), + Array(' 6 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] '), A Array(' 7 ', ' Lijiang Inn ', ' Kunming Lijiang ', ' 023-65987458 ', ' 13598784587 ', ' 1317615477 ', ' [email protected] ') at ); - //Populating table Information - for($i= 2;$i<=Count($data) + 1;$i++) { - $j= 0; - foreach($data[$i-2] as $key=$value) { - $excel->getactivesheet ()->setcellvalue ("$letter[$j]$i","$value"); in $j++; - } to } + //Create an Excel input object - $write=NewPhpexcel_writer_excel5 ($excel); the Header("Pragma:public"); * Header("expires:0"); $ Header("Cache-control:must-revalidate, post-check=0, pre-check=0");Panax Notoginseng Header("Content-type:application/force-download"); - Header("Content-type:application/vnd.ms-execl"); the Header("Content-type:application/octet-stream"); + Header("Content-type:application/download");; A Header(' content-disposition:attachment;filename= ' Inn information. xls "'); the Header("Content-transfer-encoding:binary"); + $write->save (' Php://output '); -}
The front-end code appears as follows
1 < class= "Excel"><input type= "button" value = "Export to Excel" onclick = "location.href= ' __root__/admin.php/admin/excel ';" /></ Div >
The results are as follows
This method is simple and quick, and I hope to help our friends!
Export Excel under Thinkphp