This article mainly share with you the method of PHP to create or export Excel data table, mainly in a piece of code and share with you, hope to help everyone.
$filename = "Order_". Date (' y-m-d '). ". XLS "; $header = Array (' Order number ', ' order type ', ' member id ', ' Total processing fee ', ' commodity price ', ' postage ', ' Payable amount ', ' Order status ', ' orders '); $index = Array (' ORDER_SN ', ' kind ', ' mid ', ' other_price ', ' goods_price ', ' shipping_price ', ' order_amount ', ' Order_ ' Status ', ' Regtime '); $ $orderlist = M (' table ')->where ($where)->order (' id ')->select (); Create_xls ($orderlist, $filename, $header, $index);/*** array to XLS format Excel file * @param array $data The data in the array format to be exported * @param string $filename The file name of the exported Excel table datasheet * @param a Rray $header the header of an Excel table * @param the name (key value) of the field corresponding to each item in the Excel table header $header in array $index $list arrays * For example: $header = Array (' number ') , ' name ', ' gender ', ' age '); * $index = array (' ID ', ' username ', ' sex ', ' age '); * $data = array (' ID ' =>1, ' username ' = ' yqj ', ' sex ' = ' male ', ' age ' =>24); * Sample data: $strexport = Array (Array (NULL, 12, 15, 21), Array (' Q1 ') , Array (' Q2 ', ' Up ', ', '), Array (' Q3 ', 52, 61, 69), Array (' Q4 ', 30, 32, 0),); */function Create_xls ($data, $filename = ' Simple.xls ', $header, $indexKey) {ini_set (' max_execution_time ', ' 0 '); Vendor (' Phpexcel.phpexcel '); $filename =str_replace ('. xls ', ', $filename). XLS '; $phpexcel = new Phpexcel (); $phpexcel->getproperties ()->setcreator ("Maarten Balliauw")->setlastmodifiedby ("Maarten Balliauw") ->settitle ("Office" xlsx Test Document ")->setsubject (" Office "xlsx Test Document")-& Gt;setdescription ("Test document for Office" XLSX, generated using PHP classes. ") ->setkeywords ("Office openxml PHP")->setcategory ("Test result file"); The contents of the Combined cell foreach ($data as $k + $row) {foreach ($indexKey as $key + = $value) {//This is the contents of the Set cell $strexport [$k] [$key]= $row [$value]; }} array_unshift ($strexport, $header); $phpexcel->getactivesheet ()->fromarray ($strexport); $phpexcel->getactivesheet ()->settitle (' Sheet1 '); $phpexcel->setactivesheetindex (0); Header (' Content-type:application/vnd.ms-excel '); Header ("Content-disposition:attachment;filename= $filename"); Header (' cache-control:max-age=0 '); Header (' cache-control:max-age=1 '); Header (' Expires:mon, Jul 1997 05:00:00 GMT '); Date in the past header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT '); Always modified header (' Cache-control:cache, Must-revalidate '); http/1.1 header (' pragma:public '); http/1.0 $objwriter = Phpexcel_iofactory::createwriter ($phpexcel, ' Excel5 '); $objwriter->save (' php://output '); Exit;}