• PHP导出excel


    //此方法导出excel,$data的键值必须是0,1,2...这样的数组

    public function export_cand($data,$projname,$num)
    {
            // 内存加大
            ini_set('memory_limit','2048M');
            //引入PHPExcel库文件
            include '../application/helpers/PHPExcel.php';
            //创建对象
            $excel = new PHPExcel();
            $biaotou = 'A';
            $n = 1;
            while ($n++ <= $num) {
                $biaotou++;
            }
            for($k='A'; $k != $biaotou; $k++) $letter[] = $k;
            //表头数组
            $tableheader = array('序号','人员姓名','导入时间','详情连接');
            //填充表头信息
            for( $i=0; $i < count($tableheader); $i++ ){
                $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
            }
            $excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
            //填充表格信息
            for( $i = 2; $i <= count($data) + 1; $i++){
                $j = 0;
                foreach($data[$i-2] as $key => $value){
                    $excel->getActiveSheet()->getCell('D'.$i)->getHyperlink()->setUrl('http://'.$data[$i-2][3]);
                    $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                    $j++;
                }
            }
            //创建excel输入对象
            $write = new PHPExcel_Writer_Excel2007($excel);

       //如果header头起冲突
            $filename = ini_get('upload_tmp_dir').'/cmbc_'.time().'.xlsx';
            $write->save($filename);
            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="['.$projname.'].xlsx"');
            header("Content-Transfer-Encoding:binary");
            $filepath = './canddata/cand_detail.excel';
            // $write->save('php://output');
            echo file_get_contents($filename);
            @unlink($filename);

    }

  • 相关阅读:
    beeframework开发笔记1
    CentOS 6.0最小化编译安装Nginx+MySQL+PHP+Zend
    (转)Android-Mac电脑如何进行APK反编译-使用apktool、jd-gui
    (转)【Android测试工具】03. ApkTool在Mac上的安装和使用(2.0版本)
    淘宝PHPSDK2.0 剔除 lotusphp框架---兄弟连教程
    (转载)postgresql navicat 客户端连接验证失败解决方法:password authentication failed for user
    (转载)CentOS6下 源代码方式安装openERP7.0
    在阿里云 centos 6.3上面安装php5.2(转)
    php自动转换pfx到pem和cer(dem格式)到pem
    WebSocket获取httpSession空指针异常的解决办法
  • 原文地址:https://www.cnblogs.com/wanghaodong/p/9172289.html
Copyright © 2020-2023  润新知