编什么程

phpexcel 导出xlsx文件

2018-12-26  本文已影响0人  沙蒿同学

以下封装的一个函数,activity_info的是数据,$xslx 是文件属性

<?php
  /**
         * $activity_info = [
         *      {
                    "content"   =>[
         *              {标题1,标题2,标题3,标题4},
         *              {内容1,内容2,内容3,内容4}
         *          ]
         *          "title"     => 'sheet命名'
         *      },
         *      {
                    "content"   =>[
         *              {标题1,标题2,标题3,标题4},
         *              {内容1,内容2,内容3,内容4}
         *          ]
         *          "title"     => 'sheet命名'
         *      },
         * ];
         */
  /**
     * @param array $activity_info
     * @param array $xslx
     */
    public function activityRecordExport($activity_info = array(),$xslx = array()){

        $excel = new PHPExcel();

        /* @func 设置文档基本属性 */
        $obpe_pro = $excel->getProperties();
        $obpe_pro->setCreator($xslx['seller_nick'])//设置创建者
        ->setLastModifiedBy($xslx['create_time'])//设置时间
        ->setTitle($xslx['title'])//设置标题
        ->setSubject($xslx['remark'])//设置备注
        ->setDescription($xslx['describe'])//设置描述
        ->setKeywords($xslx['keyword'])//设置关键字 | 标记
        ->setCategory($xslx['type']);//设置类别

//        /* 设置宽度 */
//        $obpe_pro->getActiveSheet()->getColumnDimension()->setAutoSize(true);
//        $obpe_pro->getActiveSheet()->getColumnDimension('B')->setWidth(10);
//        $obpe_pro->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30);
        //设置当前sheet索引,用于后续的内容操作
        //一般用在对个Sheet的时候才需要显示调用
        //缺省情况下,PHPExcel会自动创建第一个SHEET被设置SheetIndex=0
        //设置SHEET

        $cellKey = [
            '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'
        ];
        //print_r($activity_info);exit(0);
        foreach ($activity_info as $key_sheet => $value_sheet){

            $excel->createSheet();
            $excel->setactivesheetindex($key_sheet);

            $excel->getActiveSheet()->setTitle($value_sheet['tittle']);
            foreach($value_sheet['content'] as $key_column => $value){
                $count = count($value);
                $key_column = $key_column + 1;
                /* @func 设置列 */
                for ($i = 0;$i < $count; $i++){
                    $excel->getActiveSheet()->getColumnDimension($cellKey[$i])->setAutoSize(true);
                    $excel->getActiveSheet()->getStyle($cellKey[$i])->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);//水平方向上两端对齐
                    $excel->getActiveSheet()->getStyle( $cellKey[$i])->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);       //垂直方向上中间居中
                    $excel->getactivesheet()->setcellvalue($cellKey[$i].$key_column, $value[$i]);
                }
            }
        }
        $excel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$xslx['title'].'.xlsx"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
        $objWriter->save('php://output');
        exit(0);
?>

原文作者:------ 沙蒿 ------
关注我吧《程序员的碎碎念》作者,不会ui的前端不是好后端
不忘初心,牢记使命,对于文章内容有疑问的或者深入交流学习的朋友可以加我微信:2283743369

上一篇下一篇

猜你喜欢

热点阅读