In the actual development, there are many scenarios are need to export Excel table, such as background statistics, customers want to export as a form of tabular, today introduce the TP5 framework to implement Excel table Export function, first download phpexcel.zip, After decompression and put into the vendor third-party library directory, directly with the function vendor () to be introduced, directly paste code:
Public Function Pushexcelall (Request $request) {vendor (' Phpexcel. Phpexcel '); Introduce the core library file vendor (' Phpexcel. PHPExcel.Writer.Excel2007 '); Introduced the excel2007 operation class vendor (' Phpexcel. Phpexcel.iofactory '); $param = $request->param (); $id = $param [' id ']; The data id to export $NAME = Isset ($param [' name '])? $param [' name ']: ' EXAMPLEMEMB '; The Excel table name to export $DATA = Meetingmembs::query ("Select Platform,tickettype,avatar,realname,phone,company,job,ema Il,ticketremark,seatcode,id from h_meetingmembs where id = $id "); Data error_reporting (E_all) is found from the database; $objPHPExcel = new \phpexcel (); $letter = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K '); Table header Array $tableheader = Array (' Ticketing platform ', ' Ticket class ', ' Avatar ', ' name ', ' Mobile number ', ' Unit name ', ' position ', ' email ', ' remarks ', ' agent ', ' registration number '); Fill header information for ($i = 0; $i < count ($tableheader); $i + +) {$objPHPExcel->getactivesheet ()->setc Ellvalue ("$letter [$i]1", "$tableheader[$i] "); }/* The following is the processing of data in Excel, the data is taken sideways, mainly this step, the other basic do not change */foreach ($data as $k + $v) {$num = $k + 1 + 1; $objPHPExcel->setactivesheetindex (0)//excel column A, the UID is the key value of the array you isolated, and so on->setcellvalue (' A ‘ . $num, $v [' Platform '])//platform->setcellvalue (' B ' $num, $v [' TicketType '])//tickettype ->setcellvalue (' C '. $num, $v [' Avatar '])//avatar->setcellvalue (' D ' $num, $v [' realname '])//realname ->setcellvalue (' E '. $num, $v [' phone '])//phone->setcellvalue (' F '. $num, $v [' Company '])//company->setcellvalue (' G '. $num, $v [' job '])//job->setcellvalue (' H '. $num, $v [ ' Email '])//email->setcellvalue (' I '. $num, $v [' Ticketremark '])//ticketremark->setcel LValue (' J '. $num, $v [' Seatcode '])//seatcode->setcellvalue (' K ' $num, $v [' id ']);//ID} $objPHPExcel->getactivesheet ()->settitle (' Signmemb '); $objPHPExcel->setactivesheetindex (0); Header (' Content-type:application/vnd.ms-excel;charset=utf-8 '); Header (' Content-disposition:attachment;filename= '. $name. '. xls '); Header (' cache-control:max-age=0 '); $objWriter = \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 '); $objWriter->save (' php://output '); Exit }
The corresponding modification of several values, $id, $name, $data, $letter, $tableheader, and the $v property names in the Foreach loop, it's not surprising that you're done with the export feature.
PHP Export Excel Table