ThinkPHP + uploadify + upload + PHPExcel without refreshing new data import

Source: Internet
Author: User
: This article mainly introduces ThinkPHP + uploadify + upload + PHPExcel without refreshing new data import. if you are interested in the PHP Tutorial, refer to it. Front-end HTML + JQuery remarks Jquery requires version 1.x, not version 2.x

1. introduce necessary files and upload input

  
   
   
 

 

2. uploadify operation

Script $ (function () {// ThinkPHP upload address var upload = "{: U (MODULE_NAME. '/Student/upload')} "; // submission variable of ThinkPHP sessionID // 'Var _ SESSION_ID' => 'session _ id', in ThinkPHP/Conf/convention. configure in php. by default, you need to enable var sid = '{: session_id ()}'; // import data address var daoruUrl = "{: U (MODULE_NAME. '/Student/daoruhandle')} "// Amaze ui modal box var modal =$ (' # my-modal-loading '); // uploadify usage $ ('# student '). uploadify ({'swf ':'/Public/uploadify/uploadify.swf ', 'upload': upload, 'buttontext':' select a file... ', 'width': 120, 'height': 30, 'formdata': {'session _ id': sid}, 'filetypeexts ':'*. xls ', // start the upload. the pop-up modal box 'onuploadstart': function (file) {symbol ('{alert-content'character .html (' isUpload files'); Modal. modal () ;}, // Close the modal box after successful upload, and use the background function to import data 'onuploadsuccess': function (file, data, response) {certificate ('revenue alert-content'revenue .html ('data being imported '); data = eval ("(" + data + ")"); $.Ajax({Type: 'post', url: daoruUrl, data: {'file': data. file}, success: function (retdata) {modal. modal ('close'); if (retdata = 1) {alert ('Imported successfully');} else {alert ('import failed') ;}, dataType: 'json'}) ;}}) ;}); script

3. Upload operation on the ThinkPHP controller: note that the Upload. class. php space must be introduced.

Function upload () {$ config = array ('maxsize' => 3145728, 'rootpath' => '. /Uploads/', 'savepath' => '', 'savename' => array ('uniqid',''), 'exts' => array ('XLS '), 'autosub' => true, 'subname' => array ('date', 'ymmd'),); $ upload = new Upload ($ config );//Upload files$ Info = $ upload-> upload (); if (! $ Info) {// upload error message $ this-> error ($ upload-> getError ();} else {// uploaded successfullyUpload filesInformation $ file = $ info ['filedata'] ['savepath']. $ info ['filedata'] ['savename'];} // p ($ info); $ data = array ('file' => '. /Uploads /'. $ file,); echo json_encode ($ data );}

4. import data into mysql

// Import Data processing function daoruHandle () {$ file = I ('file'); $ excelData = excel_to_mysql ($ file ); foreach ($ excelData ['data'] as $ row) {$ data = array ('xuehao' => $ row ['xuehao'], 'xingming' => $ row ['xingming'], 'xingbie '=> ($ row ['xingbie'] = 'male ')? 1:0, 'Mim' => md5 ($ row ['Mim']),); M ('student ')-> add ($ data);} echo 1 ;}

5. PHPExcel function for reading Excel files and returning data

Function excel_to_mysql ($ file) {// import the PHPExcel third-party class library // vendor ('phpexcel. PHPExcel '); import ('classes. PHPExcel ', COMMON_PATH ,'. php '); // instantiate the PHPExcel class for receiving Excel files $ PHPExcel = new PHPExcel (); // read the Excel file class instantiation $ PHPReader = new PHPExcel_reader_Excel5 (); // check whether the Excel version is readable if (! $ PHPReader-> canRead ($ file) {$ PHPReader = new PHPExcel_Reader_Excel2007 (); if (! $ PHPReader-> canRead ($ file) returnarray ('error' => 1 ); // Excel of an unknown version} // read the Excel file $ PHPExcel = $ PHPReader-> load ($ file ); // Obtain the number of tables in the Excel worksheet $ sheetCount = $ PHPExcel-> getSheetCount (); // obtain the first worksheet $ sheet = $ PHPExcel-> getSheet (0 ); // obtain the maximum data column name in the table $ column = $ sheet-> getHighestColumn (); // obtain the maximum data row name in the table $ row = $ sheet-> getHighestRow ();//LoopObtain the table data for ($ I = 1; $ I <= $ row; $ I ++) {$ data [] = array (// Through the worksheetObjectGetCell method of to get the getValue of A cell to get the value of the cell 'xuehao' => $ sheet-> getCell ('A '. $ I)-> getValue (), 'xingming' => $ sheet-> getCell ('B '. $ I)-> getValue (), 'xingbie '=> $ sheet-> getCell ('C '. $ I)-> getValue (), 'Mim' => $ sheet-> getCell ('D '. $ I)-> getValue (),);} // release a worksheetObjectUnset ($ sheet); // release Read Excel fileObjectUnset ($ PHPReader); // release an Excel fileObjectUnset ($ PHPExcel); // return data returnarray ('error' => 0, 'data' => $ data );}

The above introduces ThinkPHP + uploadify + upload + PHPExcel, which allows you to smoothly import data, including related content, and hope to be helpful to anyone interested in PHP tutorials.

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.