您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)TP5.1excel導(dǎo)入數(shù)據(jù)庫的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在用之前必須 use think\PHPExcel; 并且這個(gè)是第三方類庫,這個(gè)類庫是直接用命令能后下載的 composer 命令: composer require phpoffice/phpexcel public function save(){ //前臺(tái)的form表單大家都會(huì)吧,我就不寫了 //設(shè)置文件上傳的最大限制 ini_set('memory_limit','1024M'); //加載第三方類文件 require_once "../extend/PHPExcel/PHPExcel.php"; //防止亂碼 header("Content-type:text/html;charset=utf-8"); //實(shí)例化主文件 //接收前臺(tái)傳過來的execl文件 $file = $_FILES['file']; //截取文件的后綴名,轉(zhuǎn)化成小寫 $extension = strtolower(pathinfo($file['name'],PATHINFO_EXTENSION)); if($extension == "xlsx"){ //2007(相當(dāng)于是打開接收的這個(gè)excel) $objReader =\PHPExcel_IOFactory::createReader('Excel2007'); }else{ //2003(相當(dāng)于是打開接收的這個(gè)excel) $objReader = \PHPExcel_IOFactory::createReader('Excel5'); } $objContent = $objReader -> load($file['tmp_name']); if ($objContent){ $sheetContent = $objContent -> getSheet(0) -> toArray(); //刪除第一行標(biāo)題 //dump($sheetContent);die; //其實(shí)到這里就結(jié)束了,就已經(jīng)得到excel表格的所有數(shù)據(jù)了,下面就是如何插入數(shù)據(jù)庫的問題了 ,下面我的方法大家可以參考 $len = count($sheetContent); //var_dump($len);exit; if($len == 1){ echo "<script>alert('請不要拿空表格來騙我');window.history.back();</script>"; exit(); } for($i = 1;$i<$len; $i++){ $data = [ 'id' => '', 'phone' => $sheetContent[$i][0], 'company_name' => $sheetContent[$i][1], 'add_id' => $sheetContent[$i][2], 'industry_id' => $sheetContent[$i][3], 'data_name' => $sheetContent[$i][4], 'data_address' => $sheetContent[$i][5], 'data_class' => $sheetContent[$i][6], 'data_set_up_date' => $sheetContent[$i][6], 'create_time' => date('Y-m-d') ]; //dump($data);die; $re = Db::name('data_decoration_company')->insert($data); } //var_dump($res);die; if($re){ echo "<script>alert('導(dǎo)入成功');window.history.back();</script>"; exit(); $this->redirect('/')->remember(); }else{ echo "<script>alert('導(dǎo)入失敗了,慢點(diǎn)慢點(diǎn)@!@本電腦的腦子已經(jīng)跟不上你的節(jié)奏了');window.history.back();</script>"; exit(); $this->redirect('/')->remember(); } }else{ $this->error('請導(dǎo)入表格 !'); } }
關(guān)于“TP5.1excel導(dǎo)入數(shù)據(jù)庫的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。