PHPExcel表格导出功能

14 篇文章 1 订阅

一、引入文件

标题

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二、代码如下:

function exportexcel($data,$headArr,$fileName){
    vendor("PHPExcel.Classes.PHPExcel");
    //对数据进行检验
    if(empty($data) || !is_array($data)){
        die("数据必须为数组");
    }
    //检查文件名
    if(empty($fileName)){
        exit;
    }
    //组装文件名
    $date = date("Y_m_d",time());
    $fileName .= "_{$date}.xls";

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('PRC');

    if (PHP_SAPI == 'cli')
        die('只能通过浏览器运行');

    //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
    import("Org.Util.PHPExcel");
    import("Org.Util.PHPExcel.Writer.Excel5");
    import("Org.Util.PHPExcel.IOFactory.php");

    //创建PHPExcel对象,注意,不能少了\
    $objPHPExcel = new \PHPExcel();
    $objProps = $objPHPExcel->getProperties();

    //设置表头
   $key = ord("A");//A--65
   $key2 = ord("@");//@--64</span>
   foreach($headArr as $v){
      if($key>ord("Z")){
            $key2 += 1;
            $key = ord("A");
            $colum = chr($key2).chr($key);//超过26个字母时才会启用  dingling 20150626
        }else{
            if($key2>=ord("A")){
                $colum = chr($key2).chr($key);
            }else{
                $colum = chr($key);
            }
        }
        $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', L($v['COMMENT']));
      $key += 1;
   }

   $column = 2;
   $objActSheet = $objPHPExcel->getActiveSheet();

   foreach($data as $key => $rows){ //行写入
     $span = ord("A");
      $span2 = ord("@");
      foreach($headArr as $k=>$v){
        if($span>ord("Z")){
                $span2 += 1;
                $span = ord("A");
                $j = chr($span2).chr($span);//超过26个字母时才会启用  dingling 20150626
            }else{
                if($span2>=ord("A")){
                    $j = chr($span2).chr($span);
                }else{
                    $j = chr($span);
                }
            }
            //$j = chr($span);
            $objActSheet->setCellValue($j.$column, strip_tags($rows[$v['FIELD']]));
         $span++;
      }
      $column++;
   }

   $fileName = iconv("utf-8", "gb2312", $fileName);

   // $objPHPExcel->getActiveSheet()->setTitle('CNLINK');// 重命名表
   $objPHPExcel->setActiveSheetIndex(0);// 设置活动单指数到第一个表,所以Excel打开这是第一个表


   // Redirect output to a client’s web browser (Excel5)
   header('Content-Type: application/vnd.ms-excel');
   header("Content-Disposition: attachment;filename=\"$fileName\"");
   header('Cache-Control: max-age=0');

   // If you're serving to IE 9, then the following may be needed
   header('Cache-Control: max-age=1');
   // If you're serving to IE over SSL, then the following may be needed
   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($objPHPExcel, 'Excel5');
   // $objWriter->save('1.xls');
   // echo str_replace('.php', '.xls', __FILE__);
   $objWriter->save('php://output'); //文件通过浏览器下载

   exit;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值