相思资源网 Design By www.200059.com

本文实例为大家分享了PHPExcel导出Excel表的具体代码,供大家参考,具体内容如下

/**
   * Excel导出
   * @param $fileName(文件名)
   * @param $headArr (表头)
   * @param $data  (每一行的数据)
   * @throws \PHPExcel_Exception
   * @throws \PHPExcel_Reader_Exception
   */
  function getExcel($fileName,$headArr,$data){
    include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel.php");
    include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel/Writer/Excel2007.php");
    include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel/Writer/Excel5.php");
    include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel/IOFactory.php");
    if(empty($data) || !is_array($data)){
      die("data must be a array");
    }
    if(empty($fileName)){
      exit;
    }
    $date = date("Y_m_d",time());
    $fileName .= "_{$date}.xlsx";
 
    //创建新的PHPExcel对象
    $objPHPExcel = new \PHPExcel();
    $objProps = $objPHPExcel->getProperties();
 
    //设置表头
    $key = ord("A");
    $key2 = ord("A");
    $colum2 = '';
    $objActSheet = $objPHPExcel->getActiveSheet();
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
    foreach($headArr as $v){
      $colum = chr($key);
      $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum2.$colum.'1', $v);
      if($key < 90){
        $key += 1;
      }else{
        $key = ord("A");
        $colum2 = chr($key2);
        $key2++;
      }
    }
    //exit;
    $column = 2;
 
    foreach($data as $key => $rows){ //行写入
      $span = ord("A");
      $span2 = ord("A");
      $j2 = '';
      foreach($rows as $keyName=>$value){// 列写入
        $j = chr($span);
        //$objActSheet->setCellValue($j.$column, $value);
        //把每个单元格设置成分文本类型
        //dump($j2.$j.$column);
        $objActSheet->setCellValueExplicit($j2.$j.$column,$value,\PHPExcel_Cell_DataType::TYPE_STRING);
 
        if($span < 90){
          $span += 1;
        }else{
          $span = ord("A");
          $j2 = chr($span2);
          $span2++;
        }
      }
      $column++;
    }
    // exit;
    $fileName = iconv("utf-8", "gb2312", $fileName);
    //重命名表
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    //设置活动单指数到第一个表,所以Excel打开这是第一个表
    $objPHPExcel->setActiveSheetIndex(0);
    //将输出重定向到一个客户端web浏览器(Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header("Content-Disposition: attachment; filename=\"$fileName\"");
    header('Cache-Control: max-age=0');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//    if(!empty($_GET['excel'])){
//      $objWriter->save('php://output'); //文件通过浏览器下载
//    }else{
//      $objWriter->save($fileName); //脚本方式运行,保存在当前目录
//    }
    $objWriter->save('php://output');
    exit;
 
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
PHPExcel导出Excel表,PHPExcel导出Excel,PHP导出Excel表

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“使用PHPExcel导出Excel表”

暂无使用PHPExcel导出Excel表的评论...