Home > Backend Development > PHP Tutorial > ThinkPHP+uploadify+upload+PHPExcel import data without refreshing

ThinkPHP+uploadify+upload+PHPExcel import data without refreshing

WBOY
Release: 2016-07-29 09:14:49
Original
921 people have browsed it

Front-end HTML+JQuery Note Jquery requires version 1.x, version 2.x cannot be used

1.Introduce necessary files and upload 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" />
Copy after login

<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>
Copy after login

2.uploadify usage operation

 <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('正在<strong>上传文件</strong>'<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>);
        $.<strong>ajax</strong>({
          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>
Copy after login

3. ThinkPHP Controller upload operation: Note that the Upload.class.php space needs to be introduced

<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>
Copy after login

4. Import data into 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>
Copy after login

5. PHPExcel reads the Excel file and returns the data function

<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><=<span>$row</span>;<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>
Copy after login

The above introduces ThinkPHP+uploadify+upload+PHPExcel to import data without refreshing, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template