<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件















function outExcel($data){
        $objExcel = new PHPExcel();
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
        $objActSheet = $objExcel->getActiveSheet(0);
        $objActSheet->setTitle('领取报表');//设置excel的标题
        $objActSheet->setCellValue('A1','用户id');
        $objActSheet->setCellValue('B1','红包金额（元）');
        $objActSheet->setCellValue('C1','时间');
        $objActSheet->setCellValue('D1','发放状态');

        $baseRow = 2; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
        foreach ( $data as $r => $d ) {
            $i = $baseRow + $r;
            // $objExcel->getActiveSheet()->setCellValue('A'.$i,$d['user_id']);
            // $objExcel->getActiveSheet()->setCellValue('B'.$i,$d['price']);
            // $objExcel->getActiveSheet()->setCellValue('C'.$i,$d['create_time']);
            // $objExcel->getActiveSheet()->setCellValue('D'.$i,$d['status'] == 1 ? '成功' : '失败');
        }
        $objExcel->setActiveSheetIndex(0);
        //4、输出
        $objExcel->setActiveSheetIndex();
        header('Content-Type: applicationnd.ms-excel');
        $time=date('Y-m-d');
        header("Content-Disposition: attachment;filename=领取报表$time.xls");
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
    }


/**
 * excel表格导出
 * @param string $fileName 文件名称
 * @param array $headArr 表头名称
 * @param array $data 要导出的数据
 */
if (!function_exists('excel_export')) {
    function excel_export($fileName = '', $headArr = [], $data = []) {
        $fileName .= "_" . date("Ymd", \think\facade\Request::instance()->time()) . ".xls";
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties();
        $key = ord("A"); // 设置表头
        foreach ($headArr as $v) {
            $colum = chr($key);
            //设置表头值
            $objPHPExcel->getActiveSheet()->setCellValue($colum.'1',$v);
            //设置表头字体样式
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFont()->setName('微软雅黑');
            //设置表头字体大小
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFont()->setSize(13);
            //设置表头字体是否加粗
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFont()->setBold(true);
            //设置表头文字水平居中
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置文字上下居中
            $objPHPExcel->getActiveSheet()->getStyle($colum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            //设置单元格背景色
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFill()->getStartColor()->setARGB('FFFFFFFF');
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFill()->getStartColor()->setARGB('FF6DBA43');
            //设置字体颜色
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFont()->getColor()->setARGB('FFFFFFFF');

            //设置列宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth(strlen($v)*3);
            $key += 1;
        }

        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach ($data as $key => $rows) { // 行写入
            $span = ord("A");
            foreach ($rows as $keyName => $value) { // 列写入
                if (is_numeric($value)) $value = ' '.$value;
                $objActSheet->setCellValue(chr($span) . $column, $value);
                $span++;
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
        $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename='$fileName'");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); // 文件通过浏览器下载
        exit();
    }
}
