php读取excel写入数据库,PHP读取EXCEL文件写入数据库
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)題。
- 上一篇: oracle建表空间 各种语句
- 下一篇: PHP读取Excel数据