phpexcelreader下载地址 : http://sourceforge.net/projects/phpexcelreader/
下载下来有个 “phpExcelReader.zip”的压缩包。解压,我们需要的只有 Excel 文件夹的内容,首先把 “oleread.inc” 改为 “oleread.inc.php”
修改“reader.php”文件
修改第31行 require_once 'Spreadsheet/Excel/Reader/OLERead.php';
改为 ” : require_once 'oleread.inc.php';
第 261行 =& 改为 = 号就 OK 了
源码及示例文件,以及流程都在下载文件里!
本人php新手,错误之处还请谅解。
上传文件页面!
Excel管理
上传文件处理
0) { switch ($_FILES ['upfile'] ['error']) { case 1 : $errorMsg = "上传文件超过限制!"; break; case 2 : $errorMsg = "上传文件超过前台指定大小!"; break; case 3 : $errorMsg = "上传文件不完整!"; break; case 4 : $errorMsg = "没有上传文件!"; break; } echo $errorMsg; } else { if (! is_dir ( 'upload' )) { mkdir ( 'upload' ); } //判断文件格式是否正确 $imgType = array ('application/octet-stream' ,'application/vnd.ms-excel'); if (! in_array ( $_FILES ['upfile'] ['type'], $imgType )) { echo $_FILES ['upfile'] ['type'] . "不符合文件类型"; exit (); } if (is_uploaded_file ( $_FILES ['upfile'] ['tmp_name'] )) { $toFileName = 'upload/' . $_FILES ['upfile'] ['name']; //移动临时文件到指定文件夹 if (move_uploaded_file ( $_FILES ['upfile'] ['tmp_name'], $toFileName )) { $excelFileName = $toFileName; } else { echo "文件上传失败!"; } } else { echo "不是上传文件!"; } } } else { echo "请选择上传文件!"; } ?>
解析文件组装sql
"; require_once 'reader.php'; $data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('utf8'); //设置数据库连接的用户名及密码 $conn= mysql_connect('localhost','root','root') or die("不能连接到数据库!."); //设置编码 mysql_query("set names 'utf8'"); //数据库名 mysql_select_db('excel'); //echo "是否文件=".is_file($excelFileName)."
"; if($_POST['button'] && is_file($excelFileName)){ $data->read($excelFileName); $str = explode(".",$excelFileName); $strname = explode("/",$str[0]); $tablename = $strname[1]; echo "表名:".$tablename."
"; $existsTable = existsTable($tablename); //echo $existsTable."
"; //echo print_r($data->sheets[0]['cells'][1])."
"; echo "
"; $fieldCount = count($data->sheets[0]['cells'][1]); if ($existsTable == 0) { $sql = "create table if not exists ".$tablename." (id int primary key auto_increment,"; for ($i = 1; $i<=$fieldCount; $i++) { if (!empty($data->sheets[0]['cells'][1][$i])) { $sql .= $data->sheets[0]['cells'][1][$i]." varchar(255) null,"; } } //去掉最后一个逗号 $sql = substr($sql,0,strlen($sql)-1); $sql .= ")"; echo "创建表sql=".$sql."
"; $query=mysql_query($sql); if($query){ $sign = 1; echo "创建表成功!
"; }else{ $sign = 0; echo "创建表失败!
"; return; } if ($sign == 1) { for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { /* print_r($data->sheets[0]['cells'][$i]); echo "
"; */ $sql = "insert into ".$tablename." values(null,"; for ($j = 1; $j <= count($data->sheets[0]['cells'][$i]); $j++) { $sql .= "'".$data->sheets[0]['cells'][$i][$j]."'".","; } //去掉最后一个逗号 $sql = substr($sql,0,strlen($sql)-1); $sql .= ")"; echo "插入表sql=".$sql."
"; $query = mysql_query($sql); if($query){ echo "插入表成功!
"; }else{ echo "插入表失败!
"; break; } } } } else { for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { /* print_r($data->sheets[0]['cells'][$i]); echo "
"; */ $sql = "insert into ".$tablename." values(null,"; for ($j = 1; $j <= count($data->sheets[0]['cells'][$i]); $j++) { $sql .= "'".$data->sheets[0]['cells'][$i][$j]."'".","; } //去掉最后一个逗号 $sql = substr($sql,0,strlen($sql)-1); $sql .= ")"; echo "插入表sql=".$sql."
"; $query = mysql_query($sql); if($query){ echo "插入表成功!
"; }else{ echo "插入表失败!
"; break; } } } echo "
"; } function insertData(){ } //查找是否存在表 function existsTable($tablename){ $sql="select * from ".$tablename; $query=mysql_query($sql); if($query){ return 1; }else{ return 0; } } ?>