完美结合ThinkPHP+uploadify+upload+PHPExcel实现无刷新导入数据

分类:PHP_Python| 发布:佚名| 查看:290 | 发表时间:2016/1/15

直接给大家贴代码了,代码附有注释,相信大家都能看懂的,有不明白的地方欢迎给我留言。

前端HTML+JQuery  备注Jquery需要1.x版本,不能用2.x版本

1.引入必要文件及上传input

1<load file="__PUBLIC__/js/jquery-1.11.3.min.js" />
2 <load file="__PUBLIC__/uploadify/jquery.uploadify.min.js" />
3 <load file="__PUBLIC__/uploadify/uploadify.css" />
4<input type="file" class="" id="student" name="student">

2.uploadify使用操作

01<script>
02 $(function(){
03 //ThinkPHP上传地址
04 var upload = "{:U(MODULE_NAME.'/Student/upload')}";
05 //ThinkPHP sessionID的提交变量
06 //'VAR_SESSION_ID'=>'session_id',在ThinkPHP/Conf/convention.php中进行配置,默认关闭的需要开启
07 var sid = '{:session_id()}';
08 //导入数据地址
09 var daoruUrl = "{:U(MODULE_NAME.'/Student/daoruHandle')}"
10 //Amaze ui 模态框
11 var modal = $('#my-modal-loading');
12 //uploadify使用方法
13 $('#student').uploadify({
14  'swf'  : '__PUBLIC__/uploadify/uploadify.swf',
15  'uploader' : upload,
16  'buttonText' : '选择文件...',
17  'width':120,
18  'height':30,
19  'formData':{'session_id':sid},
20  'fileTypeExts':'*.xls',
21  //开始上传弹出模态框
22  'onUploadStart' : function(file) {
23  $('#alert-content').html('正在上传文件');
24   modal.modal();
25  },
26  //上传成功关闭模态框,并用后台函数导入数据
27  'onUploadSuccess':function(file, data, response){
28  $('#alert-content').html('正在导入数据');
29  data = eval("("+data+")");
30  $.ajax({
31   type: 'POST',
32   url: daoruUrl,
33   data: {'file':data.file},
34   success: function(retdata){
35   modal.modal('close');
36   if(retdata==1){
37    alert('导入成功');
38   }else{
39    alert('导入失败');
40   }
41   },
42   dataType: 'json'
43  });
44  }
45 });
46 });
47 </script>

3、ThinkPHP控制器上传操作:备注需要引入Upload.class.php空间

01function upload(){
02 $config = array(
03  'maxSize' => 3145728,
04  'rootPath' => './Uploads/',
05  'savePath' => '',
06  'saveName' => array('uniqid',''),
07  'exts'  => array('xls'),
08  'autoSub' => true,
09  'subName' => array('date','Ymd'),
10  );
11 $upload = new Upload($config);
12 // 上传文件
13 $info = $upload->upload();
14 if(!$info) {// 上传错误提示错误信息
15  $this->error($upload->getError());
16 }else{// 上传成功 获取上传文件信息
17  $file = $info['Filedata']['savepath'].$info['Filedata']['savename'];
18 }
19 //p($info);
20 $data = array(
21  'file'=>'./Uploads/'.$file,
22  );
23 echo json_encode($data);
24}

4.导入数据进去mysql

01//导入数据处理
02 function daoruHandle(){
03  $file = I('file');
04  $excelData = excel_to_mysql($file);
05  foreach($excelData['data'] as $row){
06   $data = array(
07    'xuehao'=>$row['xuehao'],
08    'xingming'=>$row['xingming'],
09    'xingbie'=>($row['xingbie']=='男')?1:0,
10    'mima'=>md5($row['mima']),
11    );
12   M('student')->add($data);
13  }
14  echo 1;
15 }

5.PHPExcel读取Excel文件返回数据函数

01function excel_to_mysql($file){
02  //导入PHPExcel第三方类库
03  //vendor('PHPExcel.PHPExcel');
04  import('Classes.PHPExcel',COMMON_PATH,'.php');
05  //实例化PHPExcel类,用于接收Excel文件
06  $PHPExcel = new PHPExcel();
07  //读取Excel文件类实例化
08  $PHPReader = new PHPExcel_reader_Excel5();
09  //检测Excel版本是否可读
10  if(!$PHPReader->canRead($file)){
11   $PHPReader = new PHPExcel_Reader_Excel2007();
12   if(!$PHPReader->canRead($file)) return array('error'=>1);//未知版本的Excel
13  }
14  //读取Excel文件
15  $PHPExcel = $PHPReader->load($file);
16  //获得Excel中表的数量
17  $sheetCount = $PHPExcel->getSheetCount();
18  //获得第一张工作表
19  $sheet=$PHPExcel->getSheet(0);
20  //获得表中最大数据列名
21  $column = $sheet->getHighestColumn();
22  //获得表中最大数据行名
23  $row = $sheet->getHighestRow();
24  //循环获得表中数据
25  for($i=1;$i<=$row;$i++){
26   $data[] = array(
27    //通过工作表对象的getCell方法获得单元格 getValue方法获得该单元格数值
28    'xuehao'=>$sheet->getCell('A'.$i)->getValue(),
29    'xingming'=>$sheet->getCell('B'.$i)->getValue(),
30    'xingbie'=>$sheet->getCell('C'.$i)->getValue(),
31    'mima'=>$sheet->getCell('D'.$i)->getValue(),
32   );
33  }
34  //释放工作表对象
35  unset($sheet);
36  //释放读取Excel文件对象
37  unset($PHPReader);
38  //释放Excel文件对象
39  unset($PHPExcel);
40  //返回数据
41  return array('error'=>0,'data'=>$data);
42 }

通过以上代码实现了ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据,希望对你有所帮助。

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/php-python/2230.html