Excel.php 4.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: wz
 * Date: 2019/3/30
 * Time: 14:14
 */

namespace app\admin\model;


class Excel
{
    /**
     * 循环excel单元格横坐标 最大702列
     */
    public function excelAcross($num){
        $arr=['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'];
        $re=[];
        $counts=['0'=>0,'1'=>0];
        if ($num<=702){

            for ($i=0;$i<$num;$i++){

                if ($counts[0]==0){
                    $re[]=$arr[$counts[1]];

                }else{
                    $nums=$counts[0]-1;
                    $re[]=$arr[$nums].$arr[$counts[1]];

                }

                if ($counts[1]>=25){
                    $counts[1]=0;
                    $counts[0]++;
                }else{
                    $counts[1]++;
                }

            }

            return $re;
        }else{
            return false;
        }
    }

    //例子
    public function cs(){
        $title=[['列名','列宽','字段名','1:普通文本 2:图片(必须为本地路径)','图片宽度 非图片可不填']];
        $letter=$this->excelAcross(count($title));
        $list=[['id'=>1,'mobile'=>2],['id'=>2,'mobile'=>2]];
    }

    //excel

    /**
     * @param $letter
     * @param $title
     * @param $list 数据
     * @param $name
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function excel($letter,$title,$list,$name){
        // 导出Exl
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
        $objPHPExcel = new \PHPExcel();

        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);

        $objActSheet = $objPHPExcel->getActiveSheet();


        foreach ($letter as $k=>$v){
            // 水平居中(位置很重要,建议在最初始位置)
            $objPHPExcel->setActiveSheetIndex(0)->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置首行
            $objActSheet->setCellValue($v.'1', $title[$k][0]);

            // 设置个表格宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth($title[$k][1]);

            // 垂直居中
            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        }
        foreach($list as $k=>$v){
            $k +=2;
            foreach ($letter as $key=>$val){
                if ($title[$key][3]==1){
                    $objActSheet->setCellValue($val.$k, empty($v[$title[$key][2]])?'':$v[$title[$key][2]]);
                }else{
                    $objPHPExcel->getActiveSheet()->getDefaultRowDimension($k)->setRowHeight(200);
                    // 图片生成
                    $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
                    $objDrawing[$k]->setPath('./'.$v[$title[$key][2]]);
                    // 设置宽度高度
                    //$objDrawing[$k]->setHeight(80);//照片高度
                    $objDrawing[$k]->setWidth($title[$key][4]); //照片宽度
                    /*设置图片要插入的单元格*/
                    $objDrawing[$k]->setCoordinates($val.$k);
                    // 图片偏移距离
                    $objDrawing[$k]->setOffsetX(10);
                    $objDrawing[$k]->setOffsetY(10);
                    $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
                }
            }
        }

        $fileName = $name;
        $ua = $_SERVER['HTTP_USER_AGENT'];
        $ua = strtolower($ua);
        if(preg_match('/msie/', $ua) || preg_match('/edge/', $ua)) { //判断是否为IE或Edge浏览器
            $fileName = str_replace('+', '%20', urlencode($fileName)); //使用urlencode对文件名进行重新编码
        }
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xlsx";

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

    }
}