程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 詳解thinkphp實現excel數據的導入導出(附完整案例),thinkphpexcel

詳解thinkphp實現excel數據的導入導出(附完整案例),thinkphpexcel

編輯:關於PHP編程

詳解thinkphp實現excel數據的導入導出(附完整案例),thinkphpexcel


實現步驟:

一:在http://phpexcel.codeplex.com/下載最新PHPExcel放到Vendor下,注意位置:ThinkPHP\Extend\Vendor\PHPExcel\PHPExcel.php。

二:導出excel代碼實現

/**方法**/
function index(){
    $this->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");
    
    $objPHPExcel = new PHPExcel();
    $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'); 
    $objWriter->save('php://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數據代碼

function impUser(){
    if (!empty($_FILES)) {
      import("@.ORG.UploadFile");
      $config=array(
        'allowExts'=>array('xlsx','xls'),
        'savePath'=>'./Public/upload/',
        'saveRule'=>'time',
      );
      $upload = new UploadFile($config);
      if (!$upload->upload()) {
        $this->error($upload->getErrorMsg());
      } else {
        $info = $upload->getUploadFileInfo();
        
      }
    
      vendor("PHPExcel.PHPExcel");
        $file_name=$info[0]['savepath'].$info[0]['savename'];
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
        $sheet = $objPHPExcel->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 = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
          // $data['res_id']  = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
          $data['class'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
          $data['year'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
          $data['city']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
          $data['company']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
          $data['zhicheng']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
          $data['zhiwu']= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
          $data['jibie']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
          $data['honor']= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
          $data['tel']= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
          $data['qq']= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
          $data['email']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
          $data['remark']= $objPHPExcel->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("請選擇上傳的文件");
      }  
     

  }


四、模板代碼

<html>
  <head>
    
  </head>
  <body>
  <P><a href="{:U('Index/expUser')}" >導出數據並生成excel</a></P><br/>
    <form action="{:U('Index/impUser')}" method="post" enctype="multipart/form-data">
      <input type="file" name="import"/>
      <input type="hidden" name="table" value="tablename"/>
      <input type="submit" value="導入"/>
    </form>
  </body>
  
</html>

最後下載:demo下載

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved