如何利用ThinkPHP框架实现Excel数据导入和导出

  1. 首先,将PHPExcel文件放入到vendor目录下面:

    备注:ThinkPHP3.1的路径是:./ThinkPHP/Extend/Vendor/;ThinkPHP3.2的路径是:./ThinkPHP/Library/Vendor/;


    数据导出功能的实现:

  2. 编写导出为Excel的函数exportExcel:

     public function exportExcel($expTitle,$expCellName,$expTableData){
              $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
              $fileName = $_SESSION['account'].date('_YmdHis');//文件输出的文件名
              $cellNum = count($expCellName);
             $dataNum = count($expTableData);

                 vendor("PHPExcel.PHPExcel");
                $objPHPExcel = new PHPExcel();//ThinkPHP3.1的写法

                 $objPHPExcel = new  \PHPExcel();//ThinkPHP3.2的写法,有命名空间的概念
                 $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');//ThinkPHP3.1的写法

   $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//ThinkPHP3.2的写法,有命名空间的概念
  $objWriter->save('php://output');
  exit;
 }

3.读取数据,调用数据调用数据导出函数exportExcel:

function expUser(){//导出Excel
      $xlsName  = "User";
      $xlsCell  = array( //设置字段名和列名的映射
          array('id','账号序列'),
          array('truename','名字'),
          array('sex','性别'),
          array('res_id','院系'),
          array('sp_id','专业'),
          array('class','班级'),
          array('year','毕业时间'),
          array('city','所在地'),
          array('company','单位'),
          array('zhicheng','职称'),
          array('zhiwu','职务'),
          array('jibie','级别'),
          array('tel','电话'),
          array('qq','qq'),
          array('email','邮箱'),
          array('honor','荣誉'),
          array('remark','备注')
  );
  $xlsModel = M('Member');

  $xlsData  = $xlsModel->Field('id,truename,sex,res_id,sp_id,class,year,city,company,zhicheng,zhiwu,jibie,tel,qq,email,honor,remark')->select();

//将sex字段中1和0分贝装换为“男”和“女”  

foreach ($xlsData as $k => $v)
  {
       $xlsData[$k]['sex']=$v['sex']==1?'男':'女';
  }
  $this->exportExcel($xlsName,$xlsCell,$xlsData);
   
 }

你可能感兴趣的:(如何利用ThinkPHP框架实现Excel数据导入和导出)