欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现利用phpexcel导出数据

程序员文章站 2022-06-14 12:07:26
废话不多说,直接上代码吧复制代码 代码如下:public function export_data($data = array())    {...

废话不多说,直接上代码吧

复制代码 代码如下:

public function export_data($data = array())
    {
        # code...
        include_once(app_path.'tools/phpexcel/classes/phpexcel/writer/iwriter.php') ;
        include_once(app_path.'tools/phpexcel/classes/phpexcel/writer/excel5.php') ;
        include_once(app_path.'tools/phpexcel/classes/phpexcel.php') ;
        include_once(app_path.'tools/phpexcel/classes/phpexcel/iofactory.php') ;
        $obj_phpexcel = new phpexcel();
        $obj_phpexcel->getactivesheet()->setcellvalue('a1','key');
        $obj_phpexcel->getactivesheet()->setcellvalue('b1','value');       
        if($data){
            $i =2;
            foreach ($data as $key => $value) {
                # code...
                $obj_phpexcel->getactivesheet()->setcellvalue('a'.$i,$value);
                $i++;
            }
        }   

        $obj_writer = phpexcel_iofactory::createwriter($obj_phpexcel,'excel5');
        $filename = "outexcel.xls";

        header("content-type: application/force-download");
        header("content-type: application/octet-stream");
        header("content-type: application/download");
        header('content-disposition:inline;filename="'.$filename.'"');
        header("content-transfer-encoding: binary");
        header("last-modified: " . gmdate("d, d m y h:i:s") . " gmt");
        header("cache-control: must-revalidate, post-check=0, pre-check=0");
        header("pragma: no-cache");
        $obj_writer->save('php://output');
    }