PHP Excel 导出

一 下载PHPExcel

链接: https://pan.baidu.com/s/1Uv3GsbDSdsni5zDAR1412w 提取码: 4fkk 

二 将PHPExcel 引入自己的项目



三 导出

namespace app\admin\model;

class PHPExcelmodel

{

    function toExcel($list,$filename,$indexKey,$startRow=1,$excel2007=false){

        //文件引入

        // echo $pe['host_root'].'include/class/PHPExcel.php';die;

        include(LIB_PATH.'Classes/PHPExcel.php');

        include(LIB_PATH.'Classes/PHPExcel/Writer/Excel2007.php');

        // require("/include/class/PHPExcel.php");

        // include("/include/class/PHPExcel/Writer/Excel2007.php");

        ob_end_clean();

        if(empty($filename)) $filename = time();

        if( !is_array($indexKey)) return false;

        $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

        //初始化PHPExcel()

        $objPHPExcel = new \PHPExcel();

        //设置保存版本格式

        if($excel2007){

            $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);

            $filename = $filename.'.xlsx';

        }else{

            $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);

            $filename = $filename.'.xls';

        }

        //接下来就是写数据到表格里面去

        $objActSheet = $objPHPExcel->getActiveSheet();

        //$startRow = 1;

        foreach ($list as $row) {

            foreach ($indexKey as $key => $value){

                //这里是设置单元格的内容

                $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);

            }

            $startRow++;

        }

        // 下载这个表格,在浏览器输出

        header("Pragma: public");

        header("Expires: 0");

        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");

        header("Content-Type:application/force-download");

        header("Content-Type:application/vnd.ms-execl");

        header("Content-Type:application/octet-stream");

        header("Content-Type:application/download");;

        header('Content-Disposition:attachment;filename='.$filename.'');

        header("Content-Transfer-Encoding:binary");

        $objWriter->save('php://output');

    }

}

public function daochu()

    {

        $list = Db::name('goods_order')->alias('a')

            ->join('user b', 'a.user_id=b.user_id', 'Left')

            ->join('goods c', 'a.goods_id=c.goods_id', 'Left')

            ->join('goods_cate d', 'c.goods_cate_id=d.goods_cate_id', 'Left')

            ->order('a.create_time desc')

            ->field('a.goods_order_id as order_sn,c.goods_title,c.goods_price,

            d.goods_cate_name,a.goods_order_num,b.user_name,a.*,from_unixtime(a.create_time,\'%Y-%m-%d %H:%i:%s\') as addtime')

            ->select();

        $indexKey = array('order_sn','goods_title','goods_price','goods_cate_name','goods_order_num','user_name','addtime');

        //excel表头内容

        $header = array('order_sn' => '订单编号','goods_title'=>'产品名称','goods_price'=>'总金额','goods_cate_name'=>'产品分类','goods_order_num'=>'数量','user_name'=>'下单用户','addtime'=>'订单时间');

        array_unshift($list, $header);//将查询到的订单数据和表头内容合并,构造成数组list

        $res = new PHPExcelmodel();

        $res->toExcel($list, '订单excel', $indexKey, 1, true);

    }

你可能感兴趣的:(PHP Excel 导出)