本篇文章主要介绍php如何导出excel,感兴趣的朋友参考下,希望对大家有所帮助。
代码如下:
<?phperror_reporting(e_all);date_default_timezone_set('asia/shanghai');require_once './classes/phpexcel.php';$data=array( 0=>array( 'id'=>1001, 'username'=>'张飞', 'password'=>'123456', 'address'=>'三国时高老庄250巷101室' ), 1=>array( 'id'=>1002, 'username'=>'关羽', 'password'=>'123456', 'address'=>'三国时花果山' ), 2=>array( 'id'=>1003, 'username'=>'曹操', 'password'=>'123456', 'address'=>'延安西路2055弄3号' ), 3=>array( 'id'=>1004, 'username'=>'刘备', 'password'=>'654321', 'address'=>'愚园路188号3309室' ));$objphpexcel=new phpexcel();$objphpexcel->getproperties()->setcreator('http://www.jb51.net') ->setlastmodifiedby('http://www.jb51.net') ->settitle('office 2007 xlsx document') ->setsubject('office 2007 xlsx document') ->setdescription('document for office 2007 xlsx, generated using php classes.') ->setkeywords('office 2007 openxml php') ->setcategory('result file');$objphpexcel->setactivesheetindex(0) ->setcellvalue('a1','id') ->setcellvalue('b1','用户名') ->setcellvalue('c1','密码') ->setcellvalue('d1','地址');$i=2; foreach($data as $k=>$v){ $objphpexcel->setactivesheetindex(0) ->setcellvalue('a'.$i,$v['id']) ->setcellvalue('b'.$i,$v['username']) ->setcellvalue('c'.$i,$v['password']) ->setcellvalue('d'.$i,$v['address']); $i++;}$objphpexcel->getactivesheet()->settitle('三年级2班');$objphpexcel->setactivesheetindex(0);$filename=urlencode('学生信息统计表').'_'.date('y-m-dhis'); //生成xlsx文件/*header('content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('content-disposition: attachment;filename="'.$filename.'.xlsx"');header('cache-control: max-age=0');$objwriter=phpexcel_iofactory::createwriter($objphpexcel,'excel2007');*///生成xls文件header('content-type: application/vnd.ms-excel');header('content-disposition: attachment;filename="'.$filename.'.xls"');header('cache-control: max-age=0');$objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5');$objwriter->save('php://output');exit;
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php实现一段简单概率的方法
php中while循环控制的的方法及简单实例
php实现跨域操作的方法
以上就是php如何导出excel的详细内容。