ThinkPHP5.0 使用PHPExcel 导出Excel表格全过程


select();
        $tableHeader = array('ID', '名字', '性别', '手机', '额度', '创建时间', '更新时间', '删除时间', '状态');
        $this->excel($list,$tableHeader,'订单');
    }

    /**
     * 导出Excel表格
     * @param $data 打印的数据  数组形式
     * @param $tableHeader 表头 数组形式
     * @param $fileName 保存的文件名 字符串格式
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function excel($data,$tableHeader,$fileName)
    {
        //创建对象
        $excel =  new \PHPExcel();
        //Excel表格式
        $letter = 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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL',
            'AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        //填充表头信息/1是格式
        $tableHeader=$tableHeader;
        for ($i = 0; $i < count($tableHeader); $i++)
        {
            $excel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableHeader[$i]");
        }

        //填充表格信息
        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++;
            }
        }
        //创建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="'.$fileName.'.xls"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
    }
}


附图:

ThinkPHP5.0 使用PHPExcel 导出Excel表格全过程_第1张图片

你可能感兴趣的:(ThinkPHP,PHPExcel,导出)