upload excel
<?phprequire_once("include/common.php");require_once 'phpExcelReader/reader.php';$data = new Spreadsheet_Excel_Reader();$data->setOutputEncoding('UTF-8');if($_GET['file'] == 'user'){ $filename = $_FILES['myfile']['tmp_name']; $data->read($filename); /* * 用户导入顺序: 单位编号、登录名、密码、姓名、电话、备注 */ for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { //判断数据库中是否已存在用户 $username = trim($data->sheets[0]['cells'][$i][2]); $repeatsql = "select username from user where username = '$username'"; if($db->getNum($repeatsql)) break; //判断数据库中是否已存在此电话 $phone = trim($data->sheets[0]['cells'][$i][5]); $repeatsql = "select username from user where username = '$username'"; if($db->getNum($repeatsql)) break; //通过单位编码获取单位id $code = $data->sheets[0]['cells'][$i][1]; $deptsql = "select id from dept where code = '$code'"; dolog('myfile upload success'); dolog($deptsql);// fwrite(fopen("/tmp/upload.sql", 'ab+'), $deptsql."\r\n"); $rs = $db->getRow($deptsql) or die("{failure:true, msg:'<nobr>用户所在的单位编码'".$code."'不存在,请查证</nobr>'}"); $deptid = $rs['id']; //密码和姓名 $password = md5(trim($data->sheets[0]['cells'][$i][3])); $name = trim($data->sheets[0]['cells'][$i][4]); $note = trim ($data->sheets[0]['cells'][$i][6]); $sql ="INSERT INTO user(dept_id, username, password, name, phone, note) VALUES ('$deptid', '$username', '$password', '$name', '$phone', '$note')"; dolog($sql); //fwrite(fopen("/tmp/upload.sql", 'ab+'), $sql."\r\n"); $res = $db->query($sql); if (!$res) die("{failure:true, msg:'记录不符合条件,数据库错误!'}"); } } die('{success:true, file:'.json_encode($filename).'}');}if($_GET['file'] == 'dept'){ $deptfile = $_FILES['depts']['tmp_name']; $data->read($deptfile); /* * 单位导入顺序: 单位编码、上级单位编码、单位名称、备注 */ for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { $code = $data->sheets[0]['cells'][$i][1]; $parentcode = $data->sheets[0]['cells'][$i][2]; $parent = "select id from dept where code = '$parentcode'"; $parentrs = $db->getRow($parent) or die("{failure: true, msg:'<nobr>选择上级单位id出错</nobr>'}"); $deptid = $parentrs['id']; $name = $data->sheets[0]['cells'][$i][3]; //$restriction = $data->sheets[0]['cells'][$i][4]; $note = $data->sheets[0]['cells'][$i][4]; $checkcode = "select * from dept where code = '$code'"; if($db->getNum($checkcode)) break; $childsql = "select fn_get_or_create_dept('$code', '$deptid') as id"; $result = $db->getRow($childsql); $deptid = $result['id']; $sql = "update dept set name='$name', note='$note' where id = $deptid"; $db->query($sql) or die("{failure: true, msg:'<nobr>添加单位出错</nobr>'}"); } } die('{success:true, msg:'.json_encode($deptfile).'}');}if($_GET['file'] == 'che'){ $epfile = $_FILES['myfile']['tmp_name']; $data->read($epfile); //print_r($data); for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { $devid = $data->sheets[0]['cells'][$i][1]; $row = $ep->Hasdevid($devid); if($row){ $sql = "select id from ep where devid = '".$devid."'"; $devtype2 = $db->getRow($sql); $ep_id = $devtype2['id']; $sql="insert into dept_has_ep values('',167,$ep_id)"; $db->insert($sql) ; } }}if($_GET['file'] == 'ep'){ $epfile = $_FILES['myfile']['tmp_name']; $data->read($epfile); /* * 设备导入顺序:单位名称、终端号、终端名称、终端类型、基站定位、设备号、设备类型、电话、备注 */ dolog('numRows:'.$data->sheets[0]['numRows']."--"); for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { dolog('--i:'.$i."--"); $deptname = $data->sheets[0]['cells'][$i][1]; $epid = $data->sheets[0]['cells'][$i][2]; $epname = $data->sheets[0]['cells'][$i][3]; $eptype = $data->sheets[0]['cells'][$i][4]; $bsl = $data->sheets[0]['cells'][$i][5]; $devid = $data->sheets[0]['cells'][$i][6]; $devtype = $data->sheets[0]['cells'][$i][7]; //$phone = $data->sheets[0]['cells'][$i][8]; $info = $data->sheets[0]['cells'][$i][9]; $row = $ep->Hasepid($epid); if($row > 0){ dolog('warn:epid has existed '.$epid); continue;// die("{failure: true, msg:'终端号重复'}"); } $row1 = $ep->Hasdevid($devid); if($row1 > 0){ dolog('warn:devid has existed '.$devid); continue;// die("{failure: true, msg:'设备号重复'}"); } $sql = "select type from devtype where name = '".$devtype."'"; $devtype2 = $db->getRow($sql); $devtype = $devtype2['type']; if (empty($devtype) or !isset($devtype)){ dolog('warn:dev type error epid:'.$epid); continue;// die("{failure: true, msg:'设备类型出错'}"); } $sql = "select id from dept where name = '".$deptname."'"; $deptid = $db->getRow($sql); $deptid = $deptid['id']; if (empty($deptid) or !isset($deptid)){ dolog('warn:depa name error epid:'.$epid); continue;// die("{failure: true, msg:'单位名称出错'}"); } if ($bsl == '是'){ $bsl = 1; }else{ $bsl = 0; } $capability = ''; $info=$info.";&&&&"; $insert = array( 'epid' => $epid, 'name' => $epname, 'eptype' => $eptype, 'devid' => $devid, 'devtype' => $devtype, 'dept_id' => $deptid, 'bsl' => $bsl, 'info' => $info, 'capability' => $capability ); $last_insert_epid = $ep->AddEp($insert); $sql = "replace into dept_has_ep(dept_id, ep_id) values($deptid, $last_insert_epid)"; $db->query($sql) or die("{failure: true, msg:'错误003'}"); } die('{success:true, msg:'.json_encode($epfile).'}');// echo "{success:true, msg:'导入成功'}";}if($_GET['file'] == 'car'){ $epfile = $_FILES['myfile']['tmp_name']; $data->read($epfile); ini_set('max_execution_time', '1000'); /* * 设备导入顺序:单位名称、车牌号、设备类型、本次缴费金额 */ dolog('numRows:'.$data->sheets[0]['numRows']."--"); for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { dolog('--i:'.$i."--"); $name = $data->sheets[0]['cells'][$i][1]; $devid = $data->sheets[0]['cells'][$i][2]; $dev=substr((trim($devid)),0,3); if($dev!='107'){ continue; } //$eptype = $data->sheets[0]['cells'][$i][3]; $sql="select count(*) as c from ep where name ='$name'"; $q=$db->getRow($sql); $num=$q['c']; if($num){ $sql="select id from ep where name ='$name'"; $q=$db->getRow($sql); $ep_id=$q['id']; //echo $ep_id; $sql="insert into dept_has_ep values('',163,$ep_id)"; echo $sql; $db->insert($sql) ; } } //die('{success:true, msg:'.json_encode($epfile).'}');}?>
var FileFormdept = function(){};Ext.apply(FileFormdept.prototype, { uploaddeptwindow: function(){ fp = new Ext.FormPanel({ fileUpload: true, width: 400, frame: true, autoHeight: true, bodyStyle: 'padding: 10px 10px 0 10px;', labelWidth: 50, defaults: { anchor: '95%', allowBlank: false, msgTarget: 'side' }, items: [{ xtype: 'fileuploadfield', id: 'form-file', emptyText: '选择文件', fieldLabel: '文件', name: 'depts', buttonText: '浏览' }], buttons: [{ text: '上传', handler: function(){ if(fp.getForm().isValid()){ fp.getForm().submit({ url: 'file-upload.php?file=dept', waitMsg: '上传文件...', success: function(form, action){ Ext.getCmp('left-panel-refresh').fireEvent('click'); Ext.getCmp('uploaddeptwindow').close(); }, failure : function(form, action) { Ext.Msg.alert('提示',action.result.msg); } }); } } },{ text: '重置', handler: function(){ fp.getForm().reset(); } }] }); uf = new Ext.Window({ id: 'uploaddeptwindow', title: '上传要导入的文件', closable: true, modal: true, width: 410, //border:false, plain: true, items: [fp] }); uf.show(); }});
导入user能成功吗??仔细检查一下循环部分
已经解决了 谢谢 分给你吧