日期:2014-05-17 浏览次数:20514 次
<!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>Excel管理</title> <link href="css.css" rel="stylesheet" type="text/css" /> </head> <body> <form action="processExcel.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#eeeeee"><span class="text">请上传你的excel文件: <label> <input type="file" name="upfile" id="files" /> </label> <label> <input type="submit" name="button" id="button" value="上传" /> </label> </span> <label></label> <span class="text"> (注:excel的格式只能是xls) </span></td> </tr> </table> </form> </body> </html>
<?php if (! empty ( $_FILES ['upfile'] ['name'] )) { if ($_FILES ['upfile'] ['error'] > 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 "请选择上传文件!"; } ?>
<?php $excelFileName = ''; require_once 'uploadfile.php'; echo "Excel文件路径:".$excelFileName."<br>"; 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)."<br>"; if($_POST['button'] && is_file($excelFileName)){ $data->read($excelFileName); $str = explode(".",$excelFileName); $strname = explode("/",$str[0]); $tablename = $strname[1]; echo "表名:".$tablename."<br>"; $existsTable = existsTable($tablename); //echo $existsTable."<br>"; //echo print_r($data->sheets[0]['cells'][1])."<br>"; echo "<hr>"; $fieldCount = count($data->sheets[0]['cells'][1]); if ($existsTable == 0) { $sql = "create table if not exists ".$tablename