This article to introduce to you a Phpexcel read Excel and Import Database code implementation, there is a need to know the friend can refer to, here we introduce is to read the table and then create a MySQL connection, and then save to the MySQL database.
Phpexcel is a fairly powerful MS Office Excel document Generation Library, and Phpexcel is a good choice when it comes to outputting more complex format data. However, the use of the method is relatively cumbersome
| The code is as follows |
Copy Code |
Set_time_limit (20000); Ini_set (' Memory_limit ', '-1 '); Require_once './phpexcel.php '; Require_once './phpexcel/iofactory.php '; Require_once './phpexcel/reader/excel5.php '; Connect to a database using PDO $DSN = "Mysql:host=localhost;dbname=alumni;"; $user = "root"; $password = ""; try{ $DBH = new PDO ($DSN, $user, $password); $DBH->query (' Set names utf8; '); }catch (Pdoexception $e) { echo "Connection failed". $e->getmessage (); } PDO binding parameter operation $stmt = $dbh->prepare ("INSERT into alumni (Gid,student_no,name) VALUES (: Gid,:student_no,:name)"); $stmt->bindparam (": gid", $gid, PDO::P aram_str); $stmt->bindparam (": Student_no", $student _no,pdo::P aram_str); $stmt->bindparam (": Name", $name, PDO::P aram_str); $objReader = new Phpexcel_reader_excel5 (); Use excel2007 $objPHPExcel = $objReader->load (' Bks.xls '); The specified file $sheet = $objPHPExcel->getsheet (0); $highestRow = $sheet->gethighestrow (); Total number of rows obtained $highestColumn = $sheet->gethighestcolumn (); Total number of columns obtained for ($j =1; $j <=10; $j + +) { $student _no = $objPHPExcel->getactivesheet ()->getcell ("A". $j)->getvalue ();//First column number $name = $objPHPExcel->getactivesheet ()->getcell ("B". $j)->getvalue ();//second column name $gid = $objPHPExcel->getactivesheet ()->getcell ("C". $j)->getvalue ();//third-row GID } Insert the acquired Excel content into the database $stmt->execute (); ?> |
http://www.bkjia.com/PHPjc/630718.html www.bkjia.com true http://www.bkjia.com/PHPjc/630718.html techarticle this article to introduce to you a Phpexcel read Excel and Import Database code implementation, there is a need to know the friend can refer to, here we introduce is to read the table and then create a MySQL connection ...