当前位置:首页 > CMS教程 > Thinkphp > 列表

ThinkPHP 框架实现的读取excel导入数据库操作示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-27 10:25:46 浏览: 评论:0 

这篇文章主要介绍了ThinkPHP 框架实现的读取excel导入数据库操作,结合实例形式分析了thinkPHP针对Excel文件的读取、解析以及数据库的写入相关操作技巧,需要的朋友可以参考下。

本文实例讲述了ThinkPHP 框架实现的读取excel导入数据库操作,分享给大家供大家参考,具体如下:

入口文件中:

require_once VENDOR_PATH.'PHPExcel/PHPExcel/IOFactory.php';

require_once VENDOR_PATH.'PHPExcel/PHPExcel.php';

PHP:

  1. namespace Home\Controller; 
  2. class ExcelController extends CommonController 
  3. { 
  4. public function Import() { 
  5. // vendor('PHPExcel.PHPExcel.IOFactory'); 
  6. vendor("PHPExcel.PHPExcel.PHPExcel"); 
  7. vendor("PHPExcel.PHPExcel.Writer.Excel5"); 
  8. vendor("PHPExcel.PHPExcel.Writer.Excel2007"); 
  9. //$excel = new PHPExcel(); 
  10. $fileName = './trans_rate.xlsx'; 
  11. date_default_timezone_set('PRC'); 
  12. // 读取excel文件 
  13. try { 
  14. $objPHPExcel = \PHPExcel_IOFactory::load($fileName); 
  15. $inputFileType = \PHPExcel_IOFactory::identify($fileName); 
  16. $objReader = \PHPExcel_IOFactory::createReader($inputFileType); 
  17. // $objPHPExcel = $objReader->load($fileName); 
  18. // 确定要读取的sheet $sheet = $objPHPExcel->getSheet(0); 
  19. $highestRow = $sheet->getHighestRow(); 
  20. $highestColumn = $sheet->getHighestColumn(); 
  21. // 获取一行的数据 
  22. // $phone_str = ''; 
  23. for ($row = 3; $row <= $highestRow; $row++) { 
  24.  $row_data = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); 
  25. //获取excel表中一行的数组数据 
  26. //dump($row_data); 
  27. $row_data = $row_data[0]; 
  28. $time = date('Y-m-d H:i:s', strtotime(trim($row_data[0]))); 
  29. $start_province = trim($row_data[1]); 
  30. $start_city = trim($row_data[2]); 
  31. ... 
  32. // $phone_str .= '"' . $phone . '",'; 
  33. $where['phone'] = $phone; 
  34. $id_arr = M(数据表名)->where($where)->getField('id'); 
  35. $user_id = !emptyempty($id_arr) ? $id_arr : 0; 
  36. $fields[] = [ 
  37. '数据表字段' => $user_id,//用户id 
  38.           ... 
  39.            ]; 
  40. } 
  41. // dump($fields); 
  42. $rate_add = M(数据表名)->addAll($fields); 
  43. dump($rate_add); 
  44. echo M()->getLastSql(); 
  45. if (!(0 < $rate_add)) { 
  46. CommonController::logProfile('添加excel数据,SQL:' . M()->getLastSql()); $this->endBack(0); } 
  47. // echo $phone_str . '<br />'; 
  48. // dump($user_id); 
  49. } catch (Exception $e) { 
  50. die('加载文件发生错误:"' . pathinfo($fileName, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } }} 

php读取excel表数据:

  1. <?php 
  2. include 'ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php'; 
  3.  
  4. $inputFileName = './trans_rate.xlsx'; 
  5. date_default_timezone_set('PRC'); 
  6. // 读取excel文件 
  7. try { 
  8.   $inputFileType = PHPExcel_IOFactory::identify($inputFileName); 
  9.   $objReader = PHPExcel_IOFactory::createReader($inputFileType); 
  10.   $objPHPExcel = $objReader->load($inputFileName); 
  11. } catch(Exception $e) { 
  12.   die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); 
  13. } 
  14.  
  15. // 确定要读取的sheet 
  16. $sheet = $objPHPExcel->getSheet(0); 
  17. $highestRow = $sheet->getHighestRow(); 
  18. $highestColumn = $sheet->getHighestColumn(); 
  19.  
  20. // 获取一行的数据 
  21. for ($row = 1; $row <= $highestRow; $row++){ 
  22. // Read a row of data into an array 
  23. $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); 
  24. //这里得到的rowData都是一行的数据,得到数据后自行处理 
  25. var_dump($rowData); 
  26. echo "<br>"; 
  27. } 
  28. //$data为从excel中获取到的数组 
  29. for ($i =0; $i<count($data);$i++){ 
  30.   echo '<br>'; 
  31.   $gettime= explode('-',$data[$i][0]); 
  32.   if (checkdate($month=$gettime[0],$day=$gettime[1],$year=$gettime[2])){ 
  33.     echo gmdate('Y-m-d',gmmktime(0,0,0,$month,$day,$year)); 
  34.   }else{ 
  35.     echo ($data[$i][0]); 
  36.   } 
  37.   echo '-----------'; 
  38.   echo $data[$i][1]; 
  39. } 
  40.  
  41. <?php 
  42. include 'ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php'; 
  43.  
  44. $inputFileName = './test.xlsx'; 
  45. date_default_timezone_set('Asia/Shanghai'); 
  46. // 读取excel文件 
  47. try { 
  48.   $inputFileType = PHPExcel_IOFactory::identify($inputFileName); 
  49.   $objReader = PHPExcel_IOFactory::createReader($inputFileType); 
  50.   $objPHPExcel = $objReader->load($inputFileName); 
  51.  
  52.   // 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧 
  53.   $sheet = $objPHPExcel->getSheet(0); 
  54.   $highestRow = $sheet->getHighestRow();//最大行 
  55.   $highestColumn = $sheet->getHighestColumn();//最大列 
  56.  
  57.   $data = array(); 
  58.   for($rowIndex=2;$rowIndex<=$highestRow;$rowIndex++){    //循环读取每个单元格的内容。注意行从1开始,列从A开始 
  59.     for($colIndex='A';$colIndex<=$highestColumn;$colIndex++){ 
  60.       $addr = $colIndex.$rowIndex; 
  61.       if($colIndex==="A"){ //指定H列为时间所在列 
  62.         $cell = gmdate("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell($addr)->getValue())); 
  63. //        $cell = PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell($addr)->getValue()); 
  64. //        var_dump($cell);die; 
  65.       }else{ 
  66.         $cell = $sheet->getCell($addr)->getValue(); 
  67.       } 
  68. //      if($cell instanceof PHPExcel_RichText){ //富文本转换字符串 
  69. //        $cell = $cell->__toString(); 
  70. //      } 
  71.       $data[$rowIndex][$colIndex] = $cell; 
  72.     } 
  73.   } 
  74. //  return $data; 
  75.   var_dump($data); 
  76. } catch(Exception $e) { 
  77.   die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); 
  78. }

Tags: ThinkPHP读取excel导入

分享到: