Maison >développement back-end >tutoriel php >ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据
前端HTML+JQuery 备注Jquery需要1.x版本,不能用2.x版本
1.引入必要文件及上传input
<load file="__PUBLIC__/js/jquery-1.11.3.min.js"></load> <load file="__PUBLIC__/uploadify/jquery.uploadify.min.js"></load> <load file="__PUBLIC__/uploadify/uploadify.css"></load>
<span><span>input </span><span>type</span><span>="file"</span><span> class</span><span>=""</span><span> id</span><span>="student"</span><span> name</span><span>="student"</span><span>></span></span>
2.uploadify使用操作
<script><span> $(<span>function<span>(){ <span>//<span>ThinkPHP上传地址<span>var upload = "{:U(MODULE_NAME.'/Student/upload')}"<span>; <span>//<span>ThinkPHP sessionID的提交变量<span>//<span>'VAR_SESSION_ID'=>'session_id',在ThinkPHP/Conf/convention.php中进行配置,默认关闭的需要开启<span>var sid = '{:session_id()}'<span>; <span>//<span>导入数据地址<span>var daoruUrl = "{:U(MODULE_NAME.'/Student/daoruHandle')}" <span>//<span>Amaze ui 模态框<span>var modal = $('#my-modal-loading'<span>); <span>//<span>uploadify使用方法 $('#student'<span>).uploadify({ 'swf' : '__PUBLIC__/uploadify/uploadify.swf'<span>, 'uploader'<span> : upload, 'buttonText' : '选择文件...'<span>, 'width':120<span>, 'height':30<span>, 'formData':{'session_id'<span>:sid}, 'fileTypeExts':'*.xls'<span>, <span>//<span>开始上传弹出模态框 'onUploadStart' : <span>function<span>(file) { $('#alert-content').html('正在<strong>上传文件'<span>); modal.modal(); }, <span>//<span>上传成功关闭模态框,并用后台函数导入数据 'onUploadSuccess':<span>function<span>(file, data, response){ $('#alert-content').html('正在导入数据'<span>); data = eval("("+data+")"<span>); $.<strong>ajax({ type: 'POST'<span>, url: daoruUrl, data: {'file'<span>:data.file}, success: <span>function<span>(retdata){ modal.modal('close'<span>); <span>if(retdata==1<span>){ alert('导入成功'<span>); }<span>else<span>{ alert('导入失败'<span>); } }, dataType: 'json'<span> }); } }); }); </script>
3、ThinkPHP控制器上传操作:备注需要引入Upload.class.php空间
<span>function</span><span> upload(){ </span><span>$config</span> = <span>array</span><span>( </span>'maxSize' => 3145728, 'rootPath' => './Uploads/', 'savePath' => '', 'saveName' => <span>array</span>('uniqid',''), 'exts' => <span>array</span>('xls'), 'autoSub' => <span>true</span>, 'subName' => <span>array</span>('date','Ymd'),<span> ); </span><span>$upload</span> = <span>new</span> Upload(<span>$config</span><span>); </span><span>//</span><span> <strong>上传文件</strong> </span><span>$info</span> = <span>$upload</span>-><span>upload(); </span><span>if</span>(!<span>$info</span>) {<span>//</span><span> 上传错误提示错误信息</span><span>$this</span>->error(<span>$upload</span>-><span>getError()); }</span><span>else</span>{<span>//</span><span> 上传成功 获取<strong>上传文件</strong>信息</span><span>$file</span> = <span>$info</span>['Filedata']['savepath'].<span>$info</span>['Filedata']['savename'<span>]; } </span><span>//</span><span>p($info);</span><span>$data</span> = <span>array</span><span>( </span>'file'=>'./Uploads/'.<span>$file</span>,<span> ); </span><span>echo</span> json_encode(<span>$data</span><span>); }</span>
4.导入数据进去mysql
<span>//</span><span>导入数据处理</span><span>function</span><span> daoruHandle(){ </span><span>$file</span> = I('file'<span>); </span><span>$excelData</span> = excel_to_mysql(<span>$file</span><span>); </span><span>foreach</span>(<span>$excelData</span>['data'] <span>as</span><span>$row</span><span>){ </span><span>$data</span> = <span>array</span><span>( </span>'xuehao'=><span>$row</span>['xuehao'], 'xingming'=><span>$row</span>['xingming'], 'xingbie'=>(<span>$row</span>['xingbie']=='男')?1:0, 'mima'=><span>md5</span>(<span>$row</span>['mima']),<span> ); M(</span>'student')->add(<span>$data</span><span>); } </span><span>echo</span> 1<span>; }</span>
5.PHPExcel读取Excel文件返回数据函数
<span>function</span> excel_to_mysql(<span>$file</span><span>){ </span><span>//</span><span>导入PHPExcel第三方类库 //vendor('PHPExcel.PHPExcel');</span> import('Classes.PHPExcel',COMMON_PATH,'.php'<span>); </span><span>//</span><span>实例化PHPExcel类,用于接收Excel文件</span><span>$PHPExcel</span> = <span>new</span><span> PHPExcel(); </span><span>//</span><span>读取Excel文件类实例化</span><span>$PHPReader</span> = <span>new</span><span> PHPExcel_reader_Excel5(); </span><span>//</span><span>检测Excel版本是否可读</span><span>if</span>(!<span>$PHPReader</span>->canRead(<span>$file</span><span>)){ </span><span>$PHPReader</span> = <span>new</span><span> PHPExcel_Reader_Excel2007(); </span><span>if</span>(!<span>$PHPReader</span>->canRead(<span>$file</span>)) <span>return</span><span>array</span>('error'=>1);<span>//</span><span>未知版本的Excel</span><span> } </span><span>//</span><span>读取Excel文件</span><span>$PHPExcel</span> = <span>$PHPReader</span>->load(<span>$file</span><span>); </span><span>//</span><span>获得Excel中表的数量</span><span>$sheetCount</span> = <span>$PHPExcel</span>-><span>getSheetCount(); </span><span>//</span><span>获得第一张工作表</span><span>$sheet</span>=<span>$PHPExcel</span>->getSheet(0<span>); </span><span>//</span><span>获得表中最大数据列名</span><span>$column</span> = <span>$sheet</span>-><span>getHighestColumn(); </span><span>//</span><span>获得表中最大数据行名</span><span>$row</span> = <span>$sheet</span>-><span>getHighestRow(); </span><span>//</span><span><strong>循环</strong>获得表中数据</span><span>for</span>(<span>$i</span>=1;<span>$i</span>$row;<span>$i</span>++<span>){ </span><span>$data</span>[] = <span>array</span><span>( </span><span>//</span><span>通过工作表<strong>对象</strong>的getCell方法获得单元格 getValue方法获得该单元格数值</span> 'xuehao'=><span>$sheet</span>->getCell('A'.<span>$i</span>)->getValue(), 'xingming'=><span>$sheet</span>->getCell('B'.<span>$i</span>)->getValue(), 'xingbie'=><span>$sheet</span>->getCell('C'.<span>$i</span>)->getValue(), 'mima'=><span>$sheet</span>->getCell('D'.<span>$i</span>)->getValue(),<span> ); } </span><span>//</span><span>释放工作表<strong>对象</strong></span><span>unset</span>(<span>$sheet</span><span>); </span><span>//</span><span>释放读取Excel文件<strong>对象</strong></span><span>unset</span>(<span>$PHPReader</span><span>); </span><span>//</span><span>释放Excel文件<strong>对象</strong></span><span>unset</span>(<span>$PHPExcel</span><span>); </span><span>//</span><span>返回数据</span><span>return</span><span>array</span>('error'=>0,'data'=><span>$data</span><span>); }</span>
以上就介绍了ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。