How to realize Import data without Refresh based on ThinkPHP+uploadify+upload+PHPExcel
Editor to share with you how to achieve non-refresh import data based on ThinkPHP+uploadify+upload+PHPExcel. I hope you will get something after reading this article. Let's discuss it together.
1. Import necessary files and upload input
2.uploadify usage operation
(function () {/ / ThinkPHP upload address var upload = "{: U (MODULE_NAME.'/Student/upload')}"; / / the submission variable of ThinkPHP sessionID / / 'VAR_SESSION_ID'= >' session_id', is configured in ThinkPHP/Conf/convention.php. If you turn it off 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', 'uploader': upload,' buttonText': 'select file.', 'width':120,' height':30, 'formData': {' session_id':sid}, 'fileTypeExts':'*.xls' / / start uploading pop-up modal box 'onUploadStart': function (file) {$(' # alert-content'). Html ('uploading file') Modal.modal ();}, / / upload successfully closes the mode box and imports data 'onUploadSuccess':function (file, data, response) {$(' # alert-content'). Html ('importing data'); data = eval ("(" + data+ ")") $.ajax ({type: 'POST', url: daoruUrl, data: {' file':data.file}, success: function (retdata) {modal.modal ('close'); if (retdata==1) {alert (' import failed');} else {alert ('import failed');}, dataType: 'json'});})
3. Upload operation of ThinkPHP controller: remarks need to introduce Upload.class.php space.
Function upload () {$config = array ('maxSize' = > 3145728,' rootPath' = >'. / Uploads/', 'savePath' = >'', 'saveName' = > array (' uniqid',''), 'exts' = > array (' xls'), 'autoSub' = > true,' subName' = > array ('date','Ymd'),); $upload = new Upload ($config); / / upload file $info = $upload- > upload () If (! $info) {/ / upload error message $this- > error ($upload- > getError ());} else {/ / get upload file information successfully $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, 'mima'= > md5 ($row [' mima']),); M ('student')-> add ($data) } echo 1;}
5.PHPExcel reads Excel file and returns data function
Function excel_to_mysql ($file) {/ / Import PHPExcel third-party library / / vendor ('PHPExcel.PHPExcel'); import (' Classes.PHPExcel',COMMON_PATH,'.php'); / / instantiate the PHPExcel class to receive the Excel file $PHPExcel = new PHPExcel (); / / read the Excel file class instantiate $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); / / unknown version of Excel} / / read Excel file $PHPExcel = $PHPReader- > load ($file); / / get the number of tables in Excel $sheetCount = $PHPExcel- > getSheetCount (); / / get the first worksheet $sheet=$PHPExcel- > getSheet (0) / / get the most big data column name in the table $column = $sheet- > getHighestColumn (); / / get the most big data row name in the table $row = $sheet- > getHighestRow (); / / cycle to get the data for in the table GetValue (), 'xingming'= > $sheet- > getCell (' Block.Secreti)-> getValue (), 'xingbie'= > $sheet- > getCell (' Cleav.Secreti)-> getValue (), 'mima'= > $sheet- > getCell (' Demp.Secreti)-> getValue (),);} / release worksheet object unset ($sheet); / release read Excel file object unset ($PHPReader); / release Excel file object unset ($PHPExcel) / / return the data return array ('error'= > 0Gravity data channels = > $data);} after reading this article, I believe you have a certain understanding of "how to achieve non-refresh imported data based on ThinkPHP+uploadify+upload+PHPExcel". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!