日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php读取excel写入数据库,PHP读取EXCEL文件写入数据库

發(fā)布時(shí)間:2023/12/31 php 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php读取excel写入数据库,PHP读取EXCEL文件写入数据库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

PHP讀取ECEL文件寫(xiě)入數(shù)據(jù)庫(kù)功能的實(shí)現(xiàn)使用到了PHPExcel類庫(kù)。完整代碼如下:

$uploadfile="../upload_files/".basename($_FILES['userfile']['name']);

$message="";

if(@move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)){

//文件上傳服務(wù)器成功,將文件導(dǎo)入數(shù)據(jù)庫(kù)中

require_once'../libraries/PHPExcel.php';

$filePath=$uploadfile;

//從excel表格中讀取信息

$PHPExcel= newPHPExcel();

/**默認(rèn)用excel2007讀取excel,若格式不對(duì),則用之前的版本進(jìn)行讀取*/

$PHPReader= newPHPExcel_Reader_Excel2007();

if(!$PHPReader->canRead($filePath))

{

$PHPReader = newPHPExcel_Reader_Excel5();

if(!$PHPReader->canRead($filePath))

{

$message='文件不存在!';

return;

}

}

$PHPExcel= $PHPReader->load($filePath);

/**讀取excel文件中的第一個(gè)工作表*/

$currentSheet= $PHPExcel->getSheet(0);

/**取得最大的列號(hào)*/

$allColumn= $currentSheet->getHighestColumn();

/**取得一共有多少行*/

$allRow= $currentSheet->getHighestRow();

//連接數(shù)據(jù)庫(kù)

include_once'../libraries/db.php';

$db=newDb();

$conn= $db->mssqlConnetion();

if($conn ===false)

{

die( print_r( sqlsrv_errors(),true));

return NULL;

}else{//連接成功

/**從第二行開(kāi)始輸出,因?yàn)閑xcel表中第一行為列名*/

for($currentRow =2;$currentRow <= $allRow;$currentRow++)

{

$info_id= $PHPExcel->getActiveSheet()->getCell("A".$currentRow)->getValue();//獲取A列的值

$info_title= $PHPExcel->getActiveSheet()->getCell("B".$currentRow)->getValue();//獲取B列的值

$info_author= $PHPExcel->getActiveSheet()->getCell("C".$currentRow)->getValue();//獲取C列的值

$info_origin= $PHPExcel->getActiveSheet()->getCell("D".$currentRow)->getValue();//獲取D列的值

$info_theme= $PHPExcel->getActiveSheet()->getCell("E".$currentRow)->getValue();//獲取E列的值

$info_keyword= $PHPExcel->getActiveSheet()->getCell("F".$currentRow)->getValue();//獲取F列的值

$info_pubtime= $PHPExcel->getActiveSheet()->getCell("G".$currentRow)->getValue();//獲取G列的值

$info_abstract= $PHPExcel->getActiveSheet()->getCell("F".$currentRow)->getValue();//獲取H列的值

$info_format= $PHPExcel->getActiveSheet()->getCell("I".$currentRow)->getValue();//獲取I列的值

$info_isfull= $PHPExcel->getActiveSheet()->getCell("G".$currentRow)->getValue();//獲取J列的值

$info_isdown= $PHPExcel->getActiveSheet()->getCell("K".$currentRow)->getValue();//獲取K列的值

$info_trade= $PHPExcel->getActiveSheet()->getCell("L".$currentRow)->getValue();//獲取L列的值

$info_category= $PHPExcel->getActiveSheet()->getCell("M".$currentRow)->getValue();//獲取M列的值

$info_path= $PHPExcel->getActiveSheet()->getCell("N".$currentRow)->getValue();//獲取N列的值

$info_remark= $PHPExcel->getActiveSheet()->getCell("O".$currentRow)->getValue();//獲取O列的值

$info_title=iconv('utf-8','GB2312//IGNORE', $info_title);//消除亂碼

$info_author=iconv('utf-8','GB2312//IGNORE', $info_author);//消除亂碼

$info_origin=iconv('utf-8','GB2312//IGNORE', $info_origin);//消除亂碼

$info_theme=iconv('utf-8','GB2312//IGNORE', $info_theme);//消除亂碼

$info_keyword=iconv('utf-8','GB2312//IGNORE', $info_keyword);//消除亂碼

$info_pubtime=iconv('utf-8','GB2312//IGNORE', $info_pubtime);//消除亂碼

$info_abstract=iconv('utf-8','GB2312//IGNORE',$info_abstract);//消除亂碼

$info_format=iconv('utf-8','GB2312//IGNORE', $info_format);//消除亂碼

$info_isfull=iconv('utf-8','GB2312//IGNORE', $info_isfull);//消除亂碼

$info_isdown=iconv('utf-8','GB2312//IGNORE', $info_isdown);//消除亂碼

$info_trade=iconv('utf-8','GB2312//IGNORE', $info_trade);//消除亂碼

$info_category=iconv('utf-8','GB2312//IGNORE',$info_category);//消除亂碼

$info_path=iconv('utf-8','GB2312//IGNORE', $info_path);//消除亂碼

$info_remark=iconv('utf-8','GB2312//IGNORE', $info_remark);//消除亂碼

//數(shù)據(jù)庫(kù)操作

$sql= "INSERT INTO bio_Information(Info_ID,Info_Title,Info_Author,Info_Origin,Info_Theme,Info_Keyword,Info_PubTime,Info_Abstract,Info_Format,Info_IsFull,Info_IsDown,Info_Trade,Info_Category,Info_Path,Info_Remark)VALUES('".$info_id."','".$info_title."','".$info_author."','".$info_origin."','".$info_theme."','".$info_keyword."','".$info_pubtime."','".$info_abstract."','".$info_format."','".$info_isfull."','".$info_isdown."','".$info_trade."','".$info_category."','".$info_path."','".$info_remark."')";

$params= array();

$options=? array("Scrollable"=> SQLSRV_CURSOR_KEYSET );

$Stmt= sqlsrv_query( $conn, $sql , $params, $options );

if($Stmt===false)

{

die( print_r( sqlsrv_errors(),true));

return NULL;

}

}

}

//循環(huán)結(jié)束,判斷全部數(shù)據(jù)是否插入

if($currentRow >$allRow){

//echo '文件插入成功!';

$message='文件上傳成功!';

}else{

//echo '文件插入失敗!';

$message='文件上傳失敗!';

}

}else{

$message='文件上傳受阻!';

}

print"{success:true,msg:'".$message."'}";

?>

以上代碼,只實(shí)現(xiàn)了上傳入庫(kù)的功能,對(duì)于文件大小、重名、上傳失敗處理等細(xì)節(jié)未能實(shí)現(xiàn),需再次加工。

總結(jié)

以上是生活随笔為你收集整理的php读取excel写入数据库,PHP读取EXCEL文件写入数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。