phpexcel导出复杂样式excel

phpexcel导出复杂样式excel_第1张图片

后端采用tp5框架,最好使用composer在tp5根目录引入phpexcel,在vendor目录下生成,引用时‘use \PHPExcel‘即可。

具体操作为:到Packagist官网https://packagist.org/packages/phpoffice/复制phpexcel加载指令,composer require phpoffice/phpexcel,项目根目录运行。

此样式用到的类为:

use \PHPExcel;
use \PHPExcel_Style_Color;
use \PHPExcel_Style_Alignment;
use \PHPExcel_style_Fill;

具体php代码:

public function exportExcel($pid)
{
    $model = Promodel::getInstance();
    $shop = $model->shopName($pid);
    $data = $model->excData($pid);
    $objPHPExcel = new \PHPExcel();
    $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    ob_end_clean();//清空缓存
    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="表单.xls"');
    header("Content-Transfer-Encoding:binary");
    $arr = ['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'];//excel列名数组
    $col = $data['period'] * 3;//数据列数,天数三倍
    $hournum = count($data['hour']);//小时数
    //1
    $objPHPExcel->getActiveSheet()->setCellValue('A1', 'SiteName')->getColumnDimension('A')->setWidth(12);
    $objPHPExcel->getActiveSheet()->setCellValue('B1', $shop[0]['shop_name']);
    $objPHPExcel->getActiveSheet()->mergeCells('B1:' . ($arr[$col - 1]) . '1');//横向合并单元格
    //2
    $objPHPExcel->getActiveSheet()->setCellValue('A2', 'City');
    $objPHPExcel->getActiveSheet()->setCellValue('B2', '上海');
    $objPHPExcel->getActiveSheet()->mergeCells('B2:' . ($arr[$col - 1]) . '2');
    //3
    $objPHPExcel->getActiveSheet()->setCellValue('A3', 'Address');
    $objPHPExcel->getActiveSheet()->setCellValue('B3', $shop[0]['address']);
    $objPHPExcel->getActiveSheet()->mergeCells('B3:' . ($arr[$col - 1]) . '3');
    //4
    $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Date');
    //生成并赋值
    for ($i = 0; $i < $col; $i++) {
         if ($i % 3 == 0){
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '4', $data['date'][$i/3]['cdate']);
        } else {
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '4');
        }
    }
    //合并单元格
    for ($i = 0; $i < $col / 3; $i++) {
        $objPHPExcel->getActiveSheet()->mergeCells($arr[$i * 3] . '4:' . $arr[$i*3+2] . '4');
    }
   //5
    $objPHPExcel->getActiveSheet()->setCellValue('A5', $data['period'] . '天');
    //生成并赋值
    for ($i = 0; $i < $col; $i++) {
        if ($i % 3 == 0) {
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '5', $data['zhou'][$i / 3]);
        } else {
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '5');
        }
    }
    //合并单元格
    for ($i = 0; $i < $col / 3; $i++) {
        $objPHPExcel->getActiveSheet()->mergeCells($arr[$i * 3] . '5:' . $arr[$i*3+2] . '5');
    }
    //6
    $objPHPExcel->getActiveSheet()->setCellValue('A6', 'Time');
    $stet = $data['hour'][0]['chour'] . ':00' . '-' . $data['hour'][count($data['hour']) - 1]['chour'] . ':00';//时间区间
    //生成并赋值
    for ($i = 0; $i < $col; $i++) {
        if ($i % 3 == 0) {
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '6', $stet);
        } else {
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '6');
        }
    }
    //合并单元格
    for ($i = 0; $i < $col / 3; $i++) {
        $objPHPExcel->getActiveSheet()->mergeCells($arr[$i * 3] . '6:' . $arr[$i*3+2] . '6');
    }
    //7
    $objPHPExcel->getActiveSheet()->setCellValue('A7', '类别');
    //生成并赋值
    for ($i = 0; $i < $col; $i++) {
        if($i%3==0) {
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . '7', '店内客流');
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i+1] . '7', '总客流');
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i+2] . '7', '入店率');
        }
    }
    //8+
    $evehour = $data['evehour'];
    foreach ($data['hour'] as $k => $v) {
        $objPHPExcel->getActiveSheet()->setCellValue('A' . (8 + $k), $v['chour'] . ':00' . '-' . ($v['chour'] + 1) . ':00');
        for ($i = 0; $i < $col; $i++) {
            $ind = (int)floor($i / 3);
            $chour = $v['chour'];
            $cdate = $data['date'][$ind]['cdate'];
            $in=$evehour[$chour][$cdate]['in'];
            $all=$evehour[$chour][$cdate]['out'];
            $per=$evehour[$chour][$cdate]['per'];
            if ($i % 3 == 0) {
                $objPHPExcel->getActiveSheet()->setCellValue($arr[$i] . (8 + $k), $in)->getStyle($arr[$i] . (8 + $k))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->setCellValue($arr[$i+1] . (8 + $k), $all)->getStyle($arr[$i+1] . (8 + $k))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->setCellValue($arr[$i+2] . (8 + $k), $per)->getStyle($arr[$i+2] . (8 + $k))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
        }
        //设置填充的样式和背景色
        $objPHPExcel->getActiveSheet()->getStyle( $arr[0].(8 + $k).':'.$arr[$col-1].(8 + $k))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $objPHPExcel->getActiveSheet()->getStyle( $arr[0].(8 + $k).':'.$arr[$col-1].(8 + $k))->getFill()->getStartColor()->setARGB('FF00FF00');
    }

    //第8+$hournum行
    $objPHPExcel->getActiveSheet()->setCellValue('A'.(8+$hournum),'Day Total');
    for ($i = 0; $i < $col; $i++) {
        if($i%3==0){
            $in=$data['daydata'][$i*2/3]['num'];
            $all=$data['daydata'][$i*2/3]['num']+$data['daydata'][$i*2/3+1]['num'];
            $per=round($in*100/$all,2).'%';
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i].(8+$hournum),$in)->getStyle($arr[$i].(8+$hournum))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i+1].(8+$hournum),$all)->getStyle($arr[$i+1].(8+$hournum))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->setCellValue($arr[$i+2].(8+$hournum),$per)->getStyle($arr[$i+2].(8+$hournum))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        }
    }

    //设置填充的样式和背景色
    $objPHPExcel->getActiveSheet()->getStyle( $arr[0].(8 + $hournum).':'.$arr[$col-1].(8 + $hournum))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    $objPHPExcel->getActiveSheet()->getStyle( $arr[0].(8 + $hournum).':'.$arr[$col-1].(8 + $hournum))->getFill()->getStartColor()->setARGB('FFFFFF00');

    //最后一行
    $row10=(9+$hournum);
    $objPHPExcel->getActiveSheet()->setCellValue('A'.$row10, 'Week Total');
    $objPHPExcel->getActiveSheet()->setCellValue('B'.$row10, $data['sumall'])->getStyle('B'.$row10)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->mergeCells('B'.$row10.':'.$arr[$col-1].$row10);//横向合并单元格
    //设置填充的样式和背景色
    $objPHPExcel->getActiveSheet()->getStyle( 'B'.$row10)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    $objPHPExcel->getActiveSheet()->getStyle( 'B'.$row10)->getFill()->getStartColor()->setARGB('FFFF0000');

    //所有单元格居中
    $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    //单个单元格居中 $objPHPExcel->getActiveSheet()->getStyle('A1')-   >getAlignment()>setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objWriter->save('php://output');//导出

}

 

你可能感兴趣的:(php)