thinkphp5导出Excel表格例子

首先是用到了PHPExcel这个类,你可以去官网下载一个然后随便找个位置复制过去,我把他粘到了vendor里面;

excel导出是吧数据一行一行的写入到excel然后再下载下来;

第一行一般是表头:

public function excel()
 {      
        $data = array();

        //我的数据信息
         $LuntanContentModel = new \app\luntan\model\LuntanContent();   
         $contentRows = $LuntanContentModel->select();
         if($contentRows){
             foreach ($contentRows as $contentRow){
                 $info = array();
                 $row = $contentRow->toArray();

                 $info['id'] = $row['id'];
                 $info['author'] = $row['author'];
                 $info['abstract'] = $row['abstract'];
                 $info['title_simple'] = $row['title_simple'];
                 $info['title'] = $row['title'];
                 $info['browse'] = $row['browse'];
                 $data[] = $info;
             }
         }
    //定义表名称
         $name = 'ABC';

    //引入PHPExcel,注意看一下是引入的什么!并不是你下载PHPExcel 都是需要的,需要什么就拿过来什么好了
         Vendor("PHPExcel.Classes.PHPExcel"); ,
         $excel = new \PHPExcel();

//定义列数,与excel中的列的命名一样
         $letter = array('A', 'B', 'C', 'D', 'E', 'F', 'F', 'G');

//定义表头信息
         $tableheader = array('ID', '作者', '摘要', '简约标题', '标题', '浏览量');


         for ($i = 0; $i < count($tableheader); $i++)
         {
             $excel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]");
         }

//写入信息到excel
         for ($i = 2; $i <= count($data) + 1; $i++)
         {
                 $j = 0;
                 foreach ($data[$i - 2] as $key => $value)
                 {
                     $excel->getActiveSheet()->setCellValue("$letter[$j]$i", "$value");
                     $j++;
                 }
         }

// $dat的数据类型是这样的

//    array[

  //      array1[],

   //    array2[].

        …

    ]


         //创建Excel输入对象
         $write = new \PHPExcel_Writer_Excel5($excel);
         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="'.$name.'".xls"');
         header("Content-Transfer-Encoding:binary");
         $write->save('php://output');
 }    

 

144147_oG20_3720923.png

 

 

144207_AMyp_3720923.png

转载于:https://my.oschina.net/u/3720923/blog/1619642

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值