• PHP 导出到Excel表格中


        /**
         * 导出excel
         * @throws PHPExcel_Exception
         * @throws PHPExcel_Reader_Exception
         * @throws PHPExcel_Writer_Exception
         */
        public function Toexcel()
        {
            if(IS_GET){
                if($_GET['id']){
                    $bpop = M("tui_user")->where(array("u_id"=>$_GET['id']))->select();
                }
            }
            $xlsName  = "被推广人信息";
            $xlsCell  = array(
                array('u_id','推荐人姓名'),
                array('t_id','所属活动'),
                array('name','被推荐人姓名'),
                array('wxh','被推荐人微信号'),
                array('P1','所在省'),
                array('C1','所在市'),
                array('jdxx','街道详情'),
                array('time','填写时间'),
            );
            $xlsData = $bpop;
            $this->exportExcel($xlsName,$xlsCell,$xlsData);
        }
        /**
         * php 导出数据 到 excel
         * @param $expTitle
         * @param $expCellName
         * @param $expTableData
         * @throws PHPExcel_Exception
         * @throws PHPExcel_Reader_Exception
         * @throws PHPExcel_Writer_Exception
         */
        public function exportExcel($expTitle,$expCellName,$expTableData){
            $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
            //$fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
            $fileName = $xlsTitle;
            $cellNum = count($expCellName);
            $dataNum = count($expTableData);
    
            vendor("PHPExcel.PHPExcel");
    
            $objPHPExcel = new PHPExcel();
            $cellName = 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');
    
            $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
            // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
            for($i=0;$i<$cellNum;$i++){
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
            }
            // Miscellaneous glyphs, UTF-8
            for($i=0;$i<$dataNum;$i++){
                for($j=0;$j<$cellNum;$j++){
                    $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
                }
            }
    
            header('pragma:public');
            header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
            header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }


  • 相关阅读:
    CentOS Grub、BASH 故障、解决方法
    Fail2ban 阻止暴力破解
    Extundelete 数据恢复
    HTTP 状态信息
    LNMP 参数调优 ( 无注释 )
    Mysql 性能优化 ( my.cnf )
    MantisBT 缺陷管理系统
    Zabbix Agent 源码编译安装
    Zabbix 监控 Mysql 状态
    Zabbix 监控 Nginx 状态
  • 原文地址:https://www.cnblogs.com/ningjiabing/p/10066094.html
Copyright © 2020-2023  润新知