Without refreshing data import based on ThinkPHP + uploadify + upload + PHPExcel _ PHP

Source: Internet
Author: User
This article mainly introduces ThinkPHP + uploadify + upload + PHPExcel based on the latest data import information. if you need more information, paste the code directly, I believe everyone can understand the code with comments. if you have any questions, please leave me a message.

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 ', // The Start Upload pop-up modal box 'onuploadstart': function (file) {symbol ('uploalert-content'character .html ('uploading file'); 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 the file $ info = $ upload-> upload (); if (! $ Info) {// upload error message $ this-> error ($ upload-> getError ());} else {// obtain the information of the uploaded file after the upload succeeds. $ 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) return array ('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 (); // Obtain the table data cyclically ($ I = 1; $ I <= $ row; $ I ++) {$ data [] = array (// The getCell method of the worksheet object to obtain the getValue of the cell to obtain the value '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 the worksheet object unset ($ sheet); // release the unset ($ PHPReader) object for reading Excel files ); // release the Excel file object unset ($ PHPExcel); // return data return array ('error' => 0, 'data' => $ data );}

With the above code, ThinkPHP + uploadify + upload + PHPExcel can be used to smoothly import new data, hoping to help you.

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.