|
上一篇文章介紹了php-excel-reader讀取excel文件的方法,因?yàn)樾枰瑢xcel這樣的數(shù)據(jù):
php-excel-reader讀取excel內(nèi)容存入數(shù)據(jù)庫(kù) style="DISPLAY: block; MARGIN-LEFT: auto; MARGIN-RIGHT: auto" alt=php-excel-reader讀取excel內(nèi)容存入數(shù)據(jù)庫(kù) src="/d/file/itjie/phpjishu/2014-10-22/eec42bda2a0a0110f4b0877e63054950.png">新建數(shù)據(jù)庫(kù)表如下:
-- 數(shù)據(jù)庫(kù): `alumni`
-- 表的結(jié)構(gòu) `alumni`
CREATE TABLE IF NOT EXISTS `alumni` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`gid` varchar(20) DEFAULT NULL COMMENT '檔案編號(hào)',
`student_no` varchar(20) DEFAULT NULL COMMENT '學(xué)號(hào)',
`name` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `gid` (`gid`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
導(dǎo)入后數(shù)據(jù)庫(kù)結(jié)果如下:
php-excel-reader讀取excel內(nèi)容存入數(shù)據(jù)庫(kù)結(jié)果 style="DISPLAY: block; MARGIN-LEFT: auto; MARGIN-RIGHT: auto" alt=php-excel-reader讀取excel內(nèi)容存入數(shù)據(jù)庫(kù)結(jié)果 src="/d/file/itjie/phpjishu/2014-10-22/1b426e89bd7efe5cd902c3eb6884885b.png">php源碼如下:
復(fù)制代碼 代碼如下:
<?php
header("Content-Type:text/html;charset=utf-8");
require_once 'excel_reader2.php';
set_time_limit(20000);
ini_set("memory_limit","2000M");
//使用pdo連接數(shù)據(jù)庫(kù)
$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 "連接失敗".$e->getMessage();
}
//pdo綁定參數(shù)操作
$stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");
$stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
$stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
$stmt->bindParam(":name", $name,PDO::PARAM_STR);
//使用php-excel-reader讀取excel內(nèi)容
$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];
}
//將獲取的excel內(nèi)容插入到數(shù)據(jù)庫(kù)
$stmt->execute();
}
echo "執(zhí)行成功";
echo "最后插入的ID:".$dbh->lastInsertId();
?>
考慮到excel的量比較大,使用了PDO的綁定操作!
php技術(shù):php excel reader讀取excel內(nèi)容存入數(shù)據(jù)庫(kù)實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。