日期:2014-05-16 浏览次数:20607 次
<!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>上传</title>
<form method="POST" name="form" enctype="multipart/form-data">
<input name="uploadfile" type="file" id="uploadfile"/>
<input name="submit" type="submit" class="inputButton" value="批量上传"/>(通过excel上传)
</form>
</body>
</html>
<?PHP
$dir = dirname(_FILE_);//获得编译文件当前目录
$dir = str_replace("\\", "/", $dir)."/";
$filename = 'uploadfile.xls';
$path = $dir.$filename;
$result = move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path);
if ($result){
Vendor('Excel.reader');//调用第三方类,可以是require_once('指向reader.php文件所在的路径')
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');
$data->read($path);
error_reporting(E_ALL ^ E_NOTICE);
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$arr = array();
for ($j = 2; $j <= $data->sheets[0]['numCols']; $j++) {
$arr[] = $data->sheets[0]['cells'][$i][$j];
}
mysql_query("set names 'utf-8'");
$Upload = D("Upload");
$Upload>query("insert into ts_upload (userid,useremail,status,description,postdate)
values ('".$arr[0]."','".$arr[1]."','".$arr[2]."','".$arr[3]."','".$arr[4]."')");
}
unlink($path);
$this->success("上传成功!");
}else{
$this->error("上传失败!");
}