ThinkPHP 框架實現(xiàn)的讀取excel導(dǎo)入數(shù)據(jù)庫操作示例
本文實例講述了ThinkPHP 框架實現(xiàn)的讀取excel導(dǎo)入數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
入口文件中:
require_once VENDOR_PATH.'PHPExcel/PHPExcel/IOFactory.php'; require_once VENDOR_PATH.'PHPExcel/PHPExcel.php';
PHP:
namespace Home\Controller; class ExcelController extends CommonController { public function Import() { // vendor('PHPExcel.PHPExcel.IOFactory'); vendor("PHPExcel.PHPExcel.PHPExcel"); vendor("PHPExcel.PHPExcel.Writer.Excel5"); vendor("PHPExcel.PHPExcel.Writer.Excel2007"); //$excel = new PHPExcel(); $fileName = './trans_rate.xlsx'; date_default_timezone_set('PRC'); // 讀取excel文件 try { $objPHPExcel = \PHPExcel_IOFactory::load($fileName); $inputFileType = \PHPExcel_IOFactory::identify($fileName); $objReader = \PHPExcel_IOFactory::createReader($inputFileType); // $objPHPExcel = $objReader->load($fileName); // 確定要讀取的sheet $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); // 獲取一行的數(shù)據(jù) // $phone_str = ''; for ($row = 3; $row <= $highestRow; $row++) { $row_data = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); //獲取excel表中一行的數(shù)組數(shù)據(jù) //dump($row_data); $row_data = $row_data[0]; $time = date('Y-m-d H:i:s', strtotime(trim($row_data[0]))); $start_province = trim($row_data[1]); $start_city = trim($row_data[2]); ... // $phone_str .= '"' . $phone . '",'; $where['phone'] = $phone; $id_arr = M(數(shù)據(jù)表名)->where($where)->getField('id'); $user_id = !empty($id_arr) ? $id_arr : 0; $fields[] = [ '數(shù)據(jù)表字段' => $user_id,//用戶id ... ]; } // dump($fields); $rate_add = M(數(shù)據(jù)表名)->addAll($fields); dump($rate_add); echo M()->getLastSql(); if (!(0 < $rate_add)) { CommonController::logProfile('添加excel數(shù)據(jù),SQL:' . M()->getLastSql()); $this->endBack(0); } // echo $phone_str . '<br />'; // dump($user_id); } catch (Exception $e) { die('加載文件發(fā)生錯誤:"' . pathinfo($fileName, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } }}
php讀取excel表數(shù)據(jù):
<?php include 'ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php'; $inputFileName = './trans_rate.xlsx'; date_default_timezone_set('PRC'); // 讀取excel文件 try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); } catch(Exception $e) { die('加載文件發(fā)生錯誤:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); } // 確定要讀取的sheet $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); // 獲取一行的數(shù)據(jù) for ($row = 1; $row <= $highestRow; $row++){ // Read a row of data into an array $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); //這里得到的rowData都是一行的數(shù)據(jù),得到數(shù)據(jù)后自行處理 var_dump($rowData); echo "<br>"; } //$data為從excel中獲取到的數(shù)組 for ($i =0; $i<count($data);$i++){ echo '<br>'; $gettime= explode('-',$data[$i][0]); if (checkdate($month=$gettime[0],$day=$gettime[1],$year=$gettime[2])){ echo gmdate('Y-m-d',gmmktime(0,0,0,$month,$day,$year)); }else{ echo ($data[$i][0]); } echo '-----------'; echo $data[$i][1]; }
<?php include 'ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php'; $inputFileName = './test.xlsx'; date_default_timezone_set('Asia/Shanghai'); // 讀取excel文件 try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); // 確定要讀取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧 $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow();//最大行 $highestColumn = $sheet->getHighestColumn();//最大列 $data = array(); for($rowIndex=2;$rowIndex<=$highestRow;$rowIndex++){ //循環(huán)讀取每個單元格的內(nèi)容。注意行從1開始,列從A開始 for($colIndex='A';$colIndex<=$highestColumn;$colIndex++){ $addr = $colIndex.$rowIndex; if($colIndex==="A"){ //指定H列為時間所在列 $cell = gmdate("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell($addr)->getValue())); // $cell = PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell($addr)->getValue()); // var_dump($cell);die; }else{ $cell = $sheet->getCell($addr)->getValue(); } // if($cell instanceof PHPExcel_RichText){ //富文本轉(zhuǎn)換字符串 // $cell = $cell->__toString(); // } $data[$rowIndex][$colIndex] = $cell; } } // return $data; var_dump($data); } catch(Exception $e) { die('加載文件發(fā)生錯誤:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); }
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
- thinkPHP5框架導(dǎo)出Excel文件簡單操作示例
- ThinkPHP使用PHPExcel實現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出完整實例
- ThinkPHP基于PHPExcel導(dǎo)入Excel文件的方法
- 詳解thinkphp實現(xiàn)excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(附完整案例)
- ThinkPHP框架實現(xiàn)導(dǎo)出excel數(shù)據(jù)的方法示例【基于PHPExcel】
- thinkPHP實現(xiàn)將excel導(dǎo)入到數(shù)據(jù)庫中的方法
- Dwz與thinkphp整合下的數(shù)據(jù)導(dǎo)出到Excel實例
- thinkphp3.2中實現(xiàn)phpexcel導(dǎo)出帶生成圖片示例
- 基于ThinkPHP+uploadify+upload+PHPExcel 無刷新導(dǎo)入數(shù)據(jù)
- thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
- thinkphp5.1 框架導(dǎo)入/導(dǎo)出excel文件操作示例
相關(guān)文章
laravel框架添加數(shù)據(jù),顯示數(shù)據(jù),返回成功值的方法
今天小編就為大家分享一篇laravel框架添加數(shù)據(jù),顯示數(shù)據(jù),返回成功值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(八)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的公共函數(shù)部分,需要的朋友可以參考下2014-06-06Laravel框架基礎(chǔ)語法與知識點整理【模板變量、輸出、include引入子視圖等】
這篇文章主要介紹了Laravel框架基礎(chǔ)語法與知識點整理,包括模板變量、輸出、include引入子視圖等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12yii2.0框架使用 beforeAction 防非法登陸的方法分析
這篇文章主要介紹了yii2.0框架使用 beforeAction 防非法登陸的方法,結(jié)合實例形式分析了yii2.0框架中beforeAction的基本原理、用法及防止非法登陸的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09thinkPHP多域名情況下使用memcache方式共享session數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了thinkPHP多域名情況下使用memcache方式共享session數(shù)據(jù)的實現(xiàn)方法,較為詳細(xì)的分析了session的原理及多服務(wù)器共享session的相關(guān)技巧,需要的朋友可以參考下2016-07-07php+highchats生成動態(tài)統(tǒng)計圖
這篇文章主要介紹了php+highchats生成動態(tài)統(tǒng)計圖,需要的朋友可以參考下2014-05-05thinkphp調(diào)用sqlserver儲存過程返回多個結(jié)果集
這篇文章主要介紹了thinkphp調(diào)用sqlserver儲存過程返回多個結(jié)果集,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01