Heim > Backend-Entwicklung > PHP-Tutorial > ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据,thinkphpuploadify_PHP教程

ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据,thinkphpuploadify_PHP教程

WBOY
Freigeben: 2016-07-12 09:08:15
Original
712 Leute haben es durchsucht

ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据,thinkphpuploadify

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

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

  <load file="__PUBLIC__/js/jquery-1.11.3.min.js" />
  <load file="__PUBLIC__/uploadify/jquery.uploadify.min.js" />
  <load file="__PUBLIC__/uploadify/uploadify.css" />
Nach dem Login kopieren
<span><</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>
Nach dem Login kopieren

2.uploadify使用操作

 <script><span>
  $(</span><span>function</span><span>(){
    </span><span>//</span><span>ThinkPHP上传地址</span>
    <span>var</span> upload = "{:U(MODULE_NAME.'/Student/upload')}"<span>;
    </span><span>//</span><span>ThinkPHP sessionID的提交变量</span>
    <span>//</span><span>'VAR_SESSION_ID'=>'session_id',在ThinkPHP/Conf/convention.php中进行配置,默认关闭的需要开启</span>
    <span>var</span> sid = '{:session_id()}'<span>;
    </span><span>//</span><span>导入数据地址</span>
    <span>var</span> daoruUrl = "{:U(MODULE_NAME.'/Student/daoruHandle')}"
    <span>//</span><span>Amaze ui 模态框</span>
    <span>var</span> modal = $('#my-modal-loading'<span>);
    </span><span>//</span><span>uploadify使用方法</span>
    $('#student'<span>).uploadify({
      </span>'swf'      : '__PUBLIC__/uploadify/uploadify.swf'<span>,
      </span>'uploader'<span> : upload,
      </span>'buttonText' : '选择文件...'<span>,
      </span>'width':120<span>,
      </span>'height':30<span>,
      </span>'formData':{'session_id'<span>:sid},
      </span>'fileTypeExts':'*.xls'<span>,
      </span><span>//</span><span>开始上传弹出模态框</span>
      'onUploadStart' : <span>function</span><span>(file) {
        $(</span>'#alert-content').html('正在上传文件'<span>);
          modal.modal();
        },
      </span><span>//</span><span>上传成功关闭模态框,并用后台函数导入数据</span>
      'onUploadSuccess':<span>function</span><span>(file, data, response){
        $(</span>'#alert-content').html('正在导入数据'<span>);
        data </span>= eval("("+data+")"<span>);
        $.ajax({
          type: </span>'POST'<span>,
          url: daoruUrl,
          data: {</span>'file'<span>:data.file},
          success: </span><span>function</span><span>(retdata){
            modal.modal(</span>'close'<span>);
            </span><span>if</span>(retdata==1<span>){
              alert(</span>'导入成功'<span>);
            }</span><span>else</span><span>{
              alert(</span>'导入失败'<span>);
            }
          },
          dataType: </span>'json'<span>
        });
      }
    });
  });
  </span></script>
Nach dem Login kopieren

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> 上传文件 </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> 上传成功 获取上传文件信息</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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>循环获得表中数据</span>
        <span>for</span>(<span>$i</span>=1;<span>$i</span><=<span>$row</span>;<span>$i</span>++<span>){
            
            </span><span>$data</span>[] = <span>array</span><span>(
                </span><span>//</span><span>通过工作表对象的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>释放工作表对象</span>
        <span>unset</span>(<span>$sheet</span><span>);
        </span><span>//</span><span>释放读取Excel文件对象</span>
        <span>unset</span>(<span>$PHPReader</span><span>);
        </span><span>//</span><span>释放Excel文件对象</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>
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1057278.htmlTechArticleThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据,thinkphpuploadify 前端HTML+JQuery 备注Jquery需要1.x版本,不能用2.x版本 1.引入必要文件及上传input l...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage