PHP;">
PHP
header("Content-type:text/html;charset=utf-8");
//链接数据库
$link = @MysqL_connect('localhost','root','') or die('连接数据库失败');
MysqL_select_db('test',$link);
MysqL_query('set names utf8');
function upExecel(){
//判断是否选择了要上传的表格
if (empty($_POST['myfile'])) {
echo "";
}
//获取表格的大小,限制上传表格的大小5M
$file_size = $_FILES['myfile']['size'];
if ($file_size>510241024) {
echo "";
exit();
}
//限制上传表格类型
$file_type = $_FILES['myfile']['type'];
//application/vnd.ms-excel 为xls文件类型
if ($file_type!='application/vnd.ms-excel') {
echo "";
exit();
}
//判断表格是否上传成功
if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
require_once 'PHPExcel.PHP';
require_once 'PHPExcel/IOFactory.PHP';
require_once 'PHPExcel/Reader/Excel5.PHP';
//以上三步加载PHPExcel的类
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
//接收存在缓存中的excel表格
$filename = $_FILES['myfile']['tmp_name'];
$objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
// $highestColumn = $sheet->getHighestColumn(); // 取得总列数
//循环读取excel表格,读取一条,插入一条
//j表示从哪一行开始读取 从第二行开始读取,因为第一行是标题不保存
//$a表示列号
for($j=2;$j<=$highestRow;$j++)
{
$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A(业主名字)列的值
$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B(密码)列的值
$c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//获取C(手机号)列的值
$d = $objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue();//获取D(地址)列的值
//null 为主键id,自增可用null表示<a href="https://www.jb51.cc/tag/zidongtianjia/" target="_blank" class="keywords">自动添加</a>
$<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a> = "INSERT INTO house VALUES(null,'$a','$b','$c','$d')";
// echo "$<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>";
// exit();
$res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>);
if ($res) {
echo "<script>alert('<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>成功!');window.location.href='./test.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>';</script>";
}else{
echo "<script>alert('<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>失败!');window.location.href='./test.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>';</script>";
exit();
}
}
}
}
//调用
upExecel();
?>