Thinkphp+uploadify+upload+phpexcel Import data without flushing

Source: Internet
Author: User

Front Html+jquery note JQuery requires 1.x version, not 2.x version

1. Introduce necessary documents 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 "/>
<type= "File"  class= "" "  ID=" Student "  Name= "Student">

2.uploadify Use action

<script>  $(function(){    //thinkphp upload Address    varUpload = "{: U (module_name. ') /student/upload ')} "; //thinkphp SessionID's commit variable    //' var_session_id ' = ' session_id ', configured in thinkphp/conf/convention.php, default off needs to be turned on    varSid = ' {: session_id ()} '; //Import data Address    varDaoruurl = "{: U (module_name. ') /student/daoruhandle ')} "//Amaze UI Modal Box    varmodal = $ (' #my-modal-loading '); //uploadify How to use$ (' #student '). uploadify ({' swf ': ' __public__/uploadify/uploadify.swf ',      ' Uploader ': Upload,' ButtonText ': ' Select File ... ',      ' Width ': 120,      ' Height ': 30,      ' FormData ': {' session_id ': Sid},' filetypeexts ': ' *.xls ',      //start uploading pop-up modal boxes' Onuploadstart ':function(file) {$ (' #alert-content '). html (' uploading files ');        Modal.modal (); },      //upload successfully close modal box and import data using background function' Onuploadsuccess ':function(file, data, response) {$ (' #alert-content '). HTML (' importing data '); Data= eval ("(" +data+ ")"); $.ajax ({type:' POST ', Url:daoruurl, data: {' File ':d Ata.file}, Success:function(retdata) {Modal.modal (' Close '); if(retdata==1) {alert (' Import succeeded '); }Else{alert (' Import failed '); }}, DataType:' JSON '        });  }    });  }); </script>

3, thinkphp Controller upload operation: notes need to introduce Upload.class.php space

    functionupload () {$config=Array(            ' MaxSize ' = 3145728, ' RootPath ' and '/uploads/', ' savepath ' and ', ', ' Savename ' =Array(' uniqid ', '), ' exts ' =Array(' xls '), ' autosub ' =true, ' subname ' =Array(' Date ', ' YMD '),            ); $upload=NewUpload ($config); //Uploading Files        $info=$upload-upload (); if(!$info) {//Upload error message            $this->error ($upload-GetError ()); }Else{//upload successfully get upload file information                        $file=$info[' Filedata '] [' Savepath '].$info[' Filedata '] [' Savename ']; }        //p ($info);        $data=Array(            ' File ' = './uploads/'.$file,            ); EchoJson_encode ($data); }

4. Import data in MySQL

//Import Data Processing    functionDaoruhandle () {$file= I (' file '); $excelData= Excel_to_mysql ($file); foreach($excelData[' Data '] as $row){            $data=Array(                ' Xuehao ' =$row[' Xuehao '], ' xingming ' =$row[' xingming '], ' xingbie ' = ($row[' Xingbie ']== ' man ')? 1:0, ' mima ' =MD5($row[' Mima ']),                ); M (' Student ')->add ($data); }        Echo1; }

5.PHPExcel read Excel file return data function

functionExcel_to_mysql ($file){        //Import phpexcel third-party class library//vendor (' Phpexcel.phpexcel ');Import (' Classes.phpexcel ', Common_path, '. php '); //instantiate the Phpexcel class for receiving Excel files        $PHPExcel=NewPhpexcel (); //read Excel file class instantiation        $PHPReader=NewPhpexcel_reader_excel5 (); //to detect whether an Excel version is readable        if(!$PHPReader->canread ($file)){                        $PHPReader=Newphpexcel_reader_excel2007 (); if(!$PHPReader->canread ($file))return Array(' Error ' =>1);//unknown version of Excel        }        //reading Excel Files        $PHPExcel=$PHPReader->load ($file); //get the number of tables in Excel        $sheetCount=$PHPExcel-Getsheetcount (); //get the first sheet        $sheet=$PHPExcel->getsheet (0); //get the largest data column name in the table        $column=$sheet-Gethighestcolumn (); //get the largest data row name in the table        $row=$sheet-Gethighestrow (); //loop to get the data in the table         for($i= 1;$i<=$row;$i++){                        $data[] =Array(                //the cell GetValue method gets the cell value by using the Getcell method of the sheet object' Xuehao ' =$sheet->getcell (' A '.$i)->getvalue (), ' xingming ' =$sheet->getcell (' B ').$i)->getvalue (), ' xingbie ' =$sheet->getcell (' C ').$i)->getvalue (), ' mima ' =$sheet->getcell (' D '.$i)->getvalue (),                        ); }        //releasing a sheet object        unset($sheet); //frees the Read Excel file Object        unset($PHPReader); //releasing an Excel file Object        unset($PHPExcel); //Return Data        return Array(' Error ' =>0, ' data ' =$data); }

Thinkphp+uploadify+upload+phpexcel Import data without flushing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.