国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php excel reader讀取excel內容存入數據庫實現代碼

2024-05-04 23:13:18
字體:
來源:轉載
供稿:網友

上一篇文章介紹了php-excel-reader讀取excel文件的方法,因為需要,將excel這樣的數據:

php excel reader讀取excel內容存入數據庫實現代碼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綁定參數操作
$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內容
$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內容插入到數據庫
$stmt->execute();
}
echo "執行成功";
echo "最后插入的ID:".$dbh->lastInsertId();
?>


考慮到excel的量比較大,使用了PDO的綁定操作!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五大连池市| 桃源县| 柳河县| 郑州市| 蓬溪县| 开鲁县| 铜山县| 兴城市| 永兴县| 古交市| 中卫市| 渑池县| 紫阳县| 新河县| 岱山县| 嘉鱼县| 榕江县| 安西县| 曲沃县| 晋城| 巴林右旗| 革吉县| 大方县| 阿克苏市| 尼玛县| 岳阳市| 灌云县| 巴青县| 东城区| 靖西县| 五台县| 汨罗市| 德钦县| 兴隆县| 丰城市| 开平市| 吉安县| 凌海市| 合江县| 惠安县| 德化县|