根据以下代码执行,肯定不会出错。。
// index.php
//insertdb.php
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;
}
}
$dsn="mysql:dbname=me;host=127.0.0.1;charset=utf8";
$user="root";
$pwd='root';
$pdo=new PDO($dsn,$user,$pwd); //连接mysql数据库
//调用phpexcel类库
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'Classes/PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');//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&#61;2;$j<&#61;$highestRow;$j&#43;&#43;)
{
unset($arr_result);
unset($strs);
for($k&#61;&#39;A&#39;;$k<&#61; $highestColumn;$k&#43;&#43;)
{
//读取单元格
&#64;$arr_result .&#61; $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().&#39;,&#39;;
}
$strs&#61;explode(",",$arr_result);
$sql&#61;"insert into goods(id,name,content,decs) values ($strs[0],&#39;$strs[1]&#39;,&#39;$strs[2]&#39;,$strs[3])";
echo $sql."
";
$result&#61;$pdo->exec($sql) or die("执行错误");
if($result>0){
$succ_result&#43;&#61;1;
}else{
$error_result&#43;&#61;1;
}
}
echo "插入成功".$succ_result."条数据&#xff01;&#xff01;&#xff01;
";
echo "插入失败".$error_result."条数据&#xff01;&#xff01;&#xff01;";
?>