如何用thinkphp实现将excel数据导入到mysql中
1个回答
展开全部
第一步:建立数据库和数据表(按照自己的Excel数据设立字段)。
[sql] view plain copy print?
CREATE DATABASE php_excel;
USE php_excel;
CREATE TABLE IF NOT EXISTS php_excel(
id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
gid varchar(20) NOT NULL,
stu_no varchar(20) NOT NULL,
name varchar(45) NOT NULL,
age int(4) NOT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
第二步:前台index.php文件。
[html] view plain copy print?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpexcel导入excel数据到MYSQL数据库</title>
</head>
<body>
<form name="frm1" action="insertdb.php" method="post" enctype="multipart/form-data">
<input name="filename" type="file" />
<input name="submit" type="submit" value="import" />
</form>
</body>
</html>
第三步:向数据库插入数据的insertdb.php文件。
[php] view plain copy print?
session_start();
header("Content-type:text/html;charset:utf-8");
//全局变量
$succ_result=0;
$error_result=0;
$file=$_FILES['filename'];
$max_size="2000000"; //最大文件限制(单位:byte)
$fname=$file['name'];
$ftype=strtolower(substr(strrchr($fname,'.'),1));
//文件格式
$uploadfile=$file['tmp_name'];
if($_SERVER['REQUEST_METHOD']=='POST'){
if(is_uploaded_file($uploadfile)){
if($file['size']>$max_size){
echo "Import file is too large";
exit;
}
if($ftype!='xls'){
echo "Import file type is error";
exit;
}
}else{
echo "The file is not empty!";
exit;
}
}
require("./conn.php"); //连接mysql数据库
//调用phpexcel类库
require_once 'PHPExcel.php';
require_once 'PHPExcel\IOFactory.php';
require_once 'PHPExcel\Reader\Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($uploadfile);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$arr_result=array();
$strs=array();
for($j=2;$j<=$highestRow;$j++)
{
unset($arr_result);
unset($strs);
for($k='A';$k<= $highestColumn;$k++)
{
//读取单元格
$arr_result .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().',';
}
$strs=explode(",",$arr_result);
$sql="insert into php_excel(gid,stu_no,name,age) values ($strs[0],'$strs[1]','$strs[2]',$strs[3])";
echo $sql."<br/>";
mysql_query("set names utf8");
$result=mysql_query($sql) or die("执行错误");
$insert_num=mysql_affected_rows();
if($insert_num>0){
$succ_result+=1;
}else{
$error_result+=1;
}
}
echo "插入成功".$succ_result."条数据!!!<br>";
echo "插入失败".$error_result."条数据!!!";
其中conn.php代码如下:
[php] view plain copy print?
$mysql=mysql_connect("localhost","root","") or die("数据库连接失败!");
mysql_select_db("php_excel",$mysql);
mysql_query("set names utf8");
我的导入效果如下:
至此,从Excel文件读取数据批量导入到Mysql数据库完成。
[sql] view plain copy print?
CREATE DATABASE php_excel;
USE php_excel;
CREATE TABLE IF NOT EXISTS php_excel(
id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
gid varchar(20) NOT NULL,
stu_no varchar(20) NOT NULL,
name varchar(45) NOT NULL,
age int(4) NOT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
第二步:前台index.php文件。
[html] view plain copy print?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpexcel导入excel数据到MYSQL数据库</title>
</head>
<body>
<form name="frm1" action="insertdb.php" method="post" enctype="multipart/form-data">
<input name="filename" type="file" />
<input name="submit" type="submit" value="import" />
</form>
</body>
</html>
第三步:向数据库插入数据的insertdb.php文件。
[php] view plain copy print?
session_start();
header("Content-type:text/html;charset:utf-8");
//全局变量
$succ_result=0;
$error_result=0;
$file=$_FILES['filename'];
$max_size="2000000"; //最大文件限制(单位:byte)
$fname=$file['name'];
$ftype=strtolower(substr(strrchr($fname,'.'),1));
//文件格式
$uploadfile=$file['tmp_name'];
if($_SERVER['REQUEST_METHOD']=='POST'){
if(is_uploaded_file($uploadfile)){
if($file['size']>$max_size){
echo "Import file is too large";
exit;
}
if($ftype!='xls'){
echo "Import file type is error";
exit;
}
}else{
echo "The file is not empty!";
exit;
}
}
require("./conn.php"); //连接mysql数据库
//调用phpexcel类库
require_once 'PHPExcel.php';
require_once 'PHPExcel\IOFactory.php';
require_once 'PHPExcel\Reader\Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($uploadfile);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$arr_result=array();
$strs=array();
for($j=2;$j<=$highestRow;$j++)
{
unset($arr_result);
unset($strs);
for($k='A';$k<= $highestColumn;$k++)
{
//读取单元格
$arr_result .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().',';
}
$strs=explode(",",$arr_result);
$sql="insert into php_excel(gid,stu_no,name,age) values ($strs[0],'$strs[1]','$strs[2]',$strs[3])";
echo $sql."<br/>";
mysql_query("set names utf8");
$result=mysql_query($sql) or die("执行错误");
$insert_num=mysql_affected_rows();
if($insert_num>0){
$succ_result+=1;
}else{
$error_result+=1;
}
}
echo "插入成功".$succ_result."条数据!!!<br>";
echo "插入失败".$error_result."条数据!!!";
其中conn.php代码如下:
[php] view plain copy print?
$mysql=mysql_connect("localhost","root","") or die("数据库连接失败!");
mysql_select_db("php_excel",$mysql);
mysql_query("set names utf8");
我的导入效果如下:
至此,从Excel文件读取数据批量导入到Mysql数据库完成。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |