详解thinkphp实现excel数据的导入导出(附完整案例)

前端之家收集整理的这篇文章主要介绍了详解thinkphp实现excel数据的导入导出(附完整案例)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实现步骤:

一:在http://PHPexcel.codeplex.com/下载最新PHPExcel放到Vendor下,注意位置:ThinkPHP\Extend\Vendor\PHPExcel\PHPExcel.PHP

二:导出excel代码实现

display(); } public function exportExcel($expTitle,$expCellName,$expTableData){ $xlsTitle = iconv('utf-8','gb2312',$expTitle);//文件名称 $fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定 $cellNum = count($expCellName); $datanum = count($expTableData); vendor("PHPExcel.PHPExcel");
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel = new <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel();
$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');

$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
// $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->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<$d<a href="https://www.jb51.cc/tag/atan/" target="_blank" class="keywords">atan</a>um;$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 = <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_IOFactory::createWriter($obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel,'Excel5'); 
$objWriter->save('<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://output'); 
exit;  

}
/**

  • 导出Excel
    */
    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();
foreach ($xlsData as $k => $v)
{
  $xlsData[$k]['sex']=$v['sex']==1?'男':'女';
}
$this->exportExcel($xlsName,$xlsCell,$xlsData);

}

第三:导入excel数据代码

array('xlsx','xls'),'savePath'=>'./Public/upload/','saveRule'=>'time',); $upload = new UploadFile($config); if (!$upload->upload()) { $this->error($upload->getErrorMsg()); } else { $info = $upload->getUploadFileInfo();
  }

  vendor("<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel");
    $file_name=$info[0]['savepath'].$info[0]['savename'];
    $objReader = <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_IOFactory::createReader('Excel5');
    $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel = $objReader->load($file_name,$encode='utf-8');
    $sheet = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getSheet(0);
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数
    for($i=3;$i<=$highestRow;$i++)
    {  
      $data['account']= $data['truename'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); 
      $sex = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("C".$i)->getValue();
      // $data['res_id']  = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("D".$i)->getValue();
      $data['class'] = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("E".$i)->getValue();
      $data['year'] = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("F".$i)->getValue();
      $data['city']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("G".$i)->getValue();
      $data['company']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("H".$i)->getValue();
      $data['zhicheng']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("I".$i)->getValue();
      $data['zhiwu']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("J".$i)->getValue();
      $data['jibie']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("K".$i)->getValue();
      $data['honor']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("L".$i)->getValue();
      $data['tel']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("M".$i)->getValue();
      $data['qq']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("N".$i)->getValue();
      $data['email']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("O".$i)->getValue();
      $data['remark']= $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getCell("P".$i)->getValue();
      $data['sex']=$sex=='男'?1:0;
      $data['res_id'] =1;

      $data['last_login_time']=0;
      $data['create_time']=$data['last_login_ip']=$_SERVER['REMOTE_ADDR'];
      $data['login_count']=0;
      $data['join']=0;
      $data['avatar']='';
      $data['password']=md5('123456');       
      M('Member')->add($data);

    } 
     $this->success('导入成功!');
}else
  {
    $this->error("请选择<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>的<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>");
  }  

}

四、模板代码

最后下载:

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

原文链接:https://www.f2er.com/thinkphp/18174.html

猜你在找的ThinkPHP相关文章