$filename = "order_".date('y-m-d').".xls"; $header = array('订单编号','订单类型','会员id','总加工费','商品总价','邮费','应付金额','订单状态','下单时间'); $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); /** * 数组转xls格式的excel文件 * @param array $data 要导出的数组格式的数据 * @param string $filename 导出的excel表格数据表的文件名 * @param array $header excel表格的表头 * @param array $index $list数组中与excel表格表头$header中每个项目对应的字段的名字(key值) * 比如: $header = array('编号','姓名','性别','年龄'); * $index = array('id','username','sex','age'); * $data = array(array('id'=>1,'username'=>'yqj','sex'=>'男','age'=>24)); * 示例数据: $strexport = array( array(null, 2010, 2011, 2012), array('q1', 12, 15, 21), array('q2', 56, 73, 86), 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 2007 xlsx test document") ->setsubject("office 2007 xlsx test document") ->setdescription("test document for office 2007 xlsx, generated using php classes.") ->setkeywords("office 2007 openxml php") ->setcategory("test result file"); //组合单元格的内容 foreach ($data as $k=>$row) { foreach ($indexkey as $key=>$value){ //这里是设置单元格的内容 $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, 26 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; }
相关推荐:
php创建excel文件
以上就是php创建或导出excel数据表格的方法的详细内容。
