1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php读取excel写入数据库 PHP读取EXCEL文件写入数据库

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

时间:2020-01-31 13:02:18

相关推荐

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

PHP读取ECEL文件写入数据库功能的实现使用到了PHPExcel类库。完整代码如下:

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

$message="";

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

//文件上传服务器成功,将文件导入数据库中

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

$filePath=$uploadfile;

//从excel表格中读取信息

$PHPExcel= newPHPExcel();

/**默认用excel读取excel,若格式不对,则用之前的版本进行读取*/

$PHPReader= newPHPExcel_Reader_Excel();

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

{

$PHPReader = newPHPExcel_Reader_Excel5();

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

{

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

return;

}

}

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

/**读取excel文件中的第一个工作表*/

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

/**取得最大的列号*/

$allColumn= $currentSheet->getHighestColumn();

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

$allRow= $currentSheet->getHighestRow();

//连接数据库

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

$db=newDb();

$conn= $db->mssqlConnetion();

if($conn ===false)

{

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

return NULL;

}else{//连接成功

/**从第二行开始输出,因为excel表中第一行为列名*/

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);//消除乱码

//数据库操作

$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;

}

}

}

//循环结束,判断全部数据是否插入

if($currentRow >$allRow){

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

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

}else{

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

$message='文件上传失败!';

}

}else{

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

}

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

?>

以上代码,只实现了上传入库的功能,对于文件大小、重名、上传失败处理等细节未能实现,需再次加工。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。