The previous article introduced the Php-excel-reader method of reading Excel files, as needed, to put data such as Excel:
The new database table is as follows:
--Database: ' Alumni '
--The structure of the table ' alumni '
CREATE TABLE IF not EXISTS ' alumni ' (
' ID ' bigint (a) not NULL auto_increment,
' GID ' varchar (DEFAULT NULL COMMENT ' archive number '),
' student_no ' varchar DEFAULT NULL COMMENT ' study number ',
' Name ' varchar (+) DEFAULT NULL,
PRIMARY KEY (' id '),
KEY ' gid ' (' gid '),
KEY ' name ' (' name ')
) Engine=myisam DEFAULT Charset=utf8;
After importing the database results are as follows:
PHP source code is as follows:
Copy CodeThe code is as follows:
Header ("Content-type:text/html;charset=utf-8");
Require_once ' excel_reader2.php ';
Set_time_limit (20000);
Ini_set ("Memory_limit", "2000M");
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);
Reading Excel content using Php-excel-reader
$data = new Spreadsheet_excel_reader ();
$data->setoutputencoding (' UTF-8 ');
$data->read ("Stu.xls");
for ($i = 1; $i <= $data->sheets[0][' numrows '); $i + +) {
for ($j = 1; $j <= 3; $j + +) {
$student _no = $data->sheets[0][' cells ' [$i][1];
$name = $data->sheets[0][' cells ' [$i][2];
$gid = $data->sheets[0][' cells ' [$i][3];
}
Insert the acquired Excel content into the database
$stmt->execute ();
}
echo "Successful execution";
echo "Last inserted ID:". $dbh->lastinsertid ();
?>
Considering the large amount of Excel, the use of the PDO binding operation!
http://www.bkjia.com/PHPjc/326252.html www.bkjia.com true http://www.bkjia.com/PHPjc/326252.html techarticle The previous article introduced the method of Php-excel-reader reading Excel files, as needed, the data such as Excel: The new database table is as follows:--database: ' Alumni '--table structure ' a ...