您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何在PHP項(xiàng)目中利用PHPExce將Excel文件上傳到數(shù)據(jù)庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
前臺test.php頁面
<!DOCTYPE html> <html> <head> <title></title> </head> <meta charset="utf-8"> <body> <form enctype="multipart/form-data" action="./Process.php" method="post"> <table> <tr><td align="center" colspan="2"><font >上傳表格</font></td></tr> <tr><td>請先<a href="./sample/sample01.xls" rel="external nofollow" >下載excel例子模板</a>編輯后上傳文件</td></tr> <tr> <td>請選擇你要上傳的文件</td> <td><input type="file" name="myfile"></td> </tr> <tr><td><input type="submit" value="上傳文件" /></td></tr> </table> </form> </body> </html>
運(yùn)行結(jié)果:
后臺Process.php頁面
<?php header("Content-type:text/html;charset=utf-8"); //鏈接數(shù)據(jù)庫 $link = @mysql_connect('localhost','root','') or die('連接數(shù)據(jù)庫失敗'); mysql_select_db('test',$link); mysql_query('set names utf8'); function upExecel(){ //判斷是否選擇了要上傳的表格 if (empty($_POST['myfile'])) { echo "<script>alert(您未選擇表格);history.go(-1);</script>"; } //獲取表格的大小,限制上傳表格的大小5M $file_size = $_FILES['myfile']['size']; if ($file_size>5*1024*1024) { echo "<script>alert('上傳失敗,上傳的表格不能超過5M的大小');history.go(-1);</script>"; exit(); } //限制上傳表格類型 $file_type = $_FILES['myfile']['type']; //application/vnd.ms-excel 為xls文件類型 if ($file_type!='application/vnd.ms-excel') { echo "<script>alert('上傳失敗,只能上傳excel2003的xls格式!');history.go(-1)</script>"; exit(); } //判斷表格是否上傳成功 if (is_uploaded_file($_FILES['myfile']['tmp_name'])) { require_once 'PHPExcel.php'; require_once 'PHPExcel/IOFactory.php'; require_once 'PHPExcel/Reader/Excel5.php'; //以上三步加載phpExcel的類 $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format //接收存在緩存中的excel表格 $filename = $_FILES['myfile']['tmp_name']; $objPHPExcel = $objReader->load($filename); //$filename可以是上傳的表格,或者是指定的表格 $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得總行數(shù) // $highestColumn = $sheet->getHighestColumn(); // 取得總列數(shù) //循環(huán)讀取excel表格,讀取一條,插入一條 //j表示從哪一行開始讀取 從第二行開始讀取,因?yàn)榈谝恍惺菢?biāo)題不保存 //$a表示列號 for($j=2;$j<=$highestRow;$j++) { $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//獲取A(業(yè)主名字)列的值 $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//獲取B(密碼)列的值 $c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//獲取C(手機(jī)號)列的值 $d = $objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue();//獲取D(地址)列的值 //null 為主鍵id,自增可用null表示自動添加 $sql = "INSERT INTO house VALUES(null,'$a','$b','$c','$d')"; // echo "$sql"; // exit(); $res = mysql_query($sql); if ($res) { echo "<script>alert('添加成功!');window.location.href='./test.php';</script>"; }else{ echo "<script>alert('添加失敗!');window.location.href='./test.php';</script>"; exit(); } } } } //調(diào)用 upExecel(); ?>
效果為:若未選擇要上傳的文件,會提示“未選擇表格”;若表格文件超過5M,提示;若上傳的文件類型不是xls,會提示!
關(guān)于如何在PHP項(xiàng)目中利用PHPExce將Excel文件上傳到數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。