用phpExcel實現資料大量匯入到資料庫的執行個體教程

來源:互聯網
上載者:User
此例子只使用execel2003的.xls文檔,若使用的是其他版本,可以儲存格式為“Execel 97-2003 活頁簿(*.xls)”即.xls檔案類型即可!

功能說明:只能上傳Excel2003類型的xls檔案,大小不超過5M。可下載例子模板添加資料後即可上傳!

前台test.php頁面

 上傳表格<tr><td>請先<a href="./sample/sample01.xls">下載excel例子模板</a>編輯後上傳檔案</td></tr>請選擇你要上傳的檔案

運行結果:

後台Process.php頁面

<?phpheader("Content-type:text/html;charset=utf-8");//連結資料庫$link = @mysql_connect('localhost','root','') or die('串連資料庫失敗');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(); // 取得總行數     // $highestColumn = $sheet->getHighestColumn(); // 取得總列數        //迴圈讀取excel表格,讀取一條,插入一條    //j表示從哪一行開始讀取  從第二行開始讀取,因為第一行是標題不儲存    //$a表示列號for($j=2;$j<=$highestRow;$j++)      {$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//擷取A(業主名字)列的值$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//擷取B(密碼)列的值$c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//擷取C(手機號)列的值$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();        }    }}}//調用upExecel();?>

效果為:若未選擇要上傳的檔案,會提示“未選擇表格”;若表格檔案超過5M,提示;若上傳的檔案類型不是xls,會提示!

使用PHPExcel大量匯入到資料庫至此完畢,對於使用PHPExcel匯出資料可以參看使用PHPExcel實現資料大量匯出為excel表格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.