溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php excel reader怎么實(shí)現(xiàn)讀取excel內(nèi)容存入數(shù)據(jù)庫的方法

發(fā)布時(shí)間:2021-02-01 10:09:12 來源:億速云 閱讀:156 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹php excel reader怎么實(shí)現(xiàn)讀取excel內(nèi)容存入數(shù)據(jù)庫的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

php-excel-reader讀取excel文件的方法,因?yàn)樾枰?,將excel這樣的數(shù)據(jù):

php excel reader怎么實(shí)現(xiàn)讀取excel內(nèi)容存入數(shù)據(jù)庫的方法

新建數(shù)據(jù)庫表如下:

-- 數(shù)據(jù)庫: `alumni`
-- 表的結(jié)構(gòu) `alumni`
CREATE TABLE IF NOT EXISTS `alumni` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `gid` varchar(20) DEFAULT NULL COMMENT '檔案編號',
  `student_no` varchar(20) DEFAULT NULL COMMENT '學(xué)號',
  `name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `gid` (`gid`),
  KEY `name` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

導(dǎo)入后數(shù)據(jù)庫結(jié)果如下:

php excel reader怎么實(shí)現(xiàn)讀取excel內(nèi)容存入數(shù)據(jù)庫的方法

php源碼如下:

<?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ù)庫 
$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ù)庫 
$stmt->execute(); 
} 
echo "執(zhí)行成功"; 
echo "最后插入的ID:".$dbh->lastInsertId(); 
?>

以上是“php excel reader怎么實(shí)現(xiàn)讀取excel內(nèi)容存入數(shù)據(jù)庫的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI