phpexcel--reading Excel
Last Update:2017-02-28
Source: Internet
Author: User
The main function is to read uploaded Excel files, insert or update to the database. Iconv ("GBK", "UTF8", $_files["file"] ["Tmp_name"]), mysql_query ("SET NAMES ' UTF8 '"), code conversion to prevent Chinese in the database display garbled; The following are the main source code: Copy Code 1 header ("Content-type:text/html;charset=utf-8"); 2 require_once (' phpexcel/classes/phpexcel.php '); 3 require_once (' phpexcel/classes/phpexcel/iofactory.php '); 4 5 iconv ("GBK", "UTF8", $_files["file"] ["tmp_name"])/code conversion 6 7 $fileType = Phpexcel_iofactory::identify ($_files["file"] ["tmp_name"])//Determine the type of file 8 $objReader =phpexcel_iofactory:: Createreader ($fileType);//Create object 9 $objPHPExcel = $objReader->load ($_files[' file ' [' Tmp_name '])//Import data 10 $currentSheet = $objPHPExcel->getsheet (0); The first workbook $allRow = $currentSheet->gethighestrow (); Number of rows $letters _arr = Array (1=> ' A ',2=> ' B ',3=> ' C ',4=> ' D ',5=> ' E ',6=> ' F ',7=> ' G ',8=> ' H '), 9 = > ' I ',10=> ' J ',11=> ' K ',12=> ' L ',13=> ' M ', 14=> ' N ',15=> ' O ',16=> ' P ',17=> ' Q ',18=> ' R',19=> ' S ',20=> ' T ',21=> ' U ',22=> ' V ',23=> ' W ',24=> ' X ',25=> ' Y ',26=> ' Z '); $con =mysql_connect (' localhost ', ' root ', ' root ') or Die (' Database link failed! <br/> '. mysql_error ()); 18 $db =mysql_select_db (' db_product ', $con); mysql_query ("SET NAMES ' UTF8 '"); for ($currentRow = 3; $currentRow <= $allRow; $currentRow + +) { $rowValues = ' $id = (string) ($ Currentsheet->getcell (' C '. $currentRow)->getvalue ()); 24 $select _id=mysql_query ("select * from T_shop WHERE productnum= ' $id '"); $result =mysql_f Etch_array ($select _id); if (! $result) {//Determine if there is a foreach ($letters _arr as $key => $value) {3 1 $XH = $currentSheet->getcell ($value. $currentRow)->getvalue (); if ($key >14) break; $rowValues = $rowValues. ($key >1 ', ': '). ' \ '. $xh. ' \''; 34 35 } Notoginseng $sql = "INSERT into T_shop (Shopname,brand,productnum,spec,barcode,category,productname,ordernumber, Deliverynumber,returnnumber,returnbacknumber,salesprice,costprice,profit) VALUES (". $rowValues.") "; mysql_query ($sql); ($result, $letters _arr, $currentSheet, $c else{ $reValues Urrentrow); $sql = "UPDATE t_shop SET shopname= ' $reValues [0] ', brand= ' $reValues [1] ', spec= ' $reValues [2] ', barcode= ' $ Revalues[3] ', category= ' $reValues [4] ', Productname= ' $reValues [5] ', ordernumber= $reValues [6],deliverynumber=$ revalues[7],returnnumber= $reValues [8],returnbacknumber= $reValues [9],salesprice= $reValues [10],costprice=$ Revalues[11],profit= $reValues [A] WHERE productnum= ' $id '; mysql_query ($sql); Echo mysql_error (). $id. ' ###'; Print_r ($reValues); echo ' <br/> '; } 52 Wuyi &nbsP Mysql_close ($con); 56 function Re_row ($s, $a, $obj, $currentRow) { $b = $d = '; ' foreach ($a as $key => $value) { $XH = $obj->getcell ($value. $currentRow)->getvalue (); &NB if ($key >14) break; Sp $d []= $xh; 63 64 } 66 foreach ($d as $key => $value) {!=2 if ($key &A mp;& $key <7) { $b []= (String) $value; }, ElseIf ($key >6) { $b []= $s [$key]+ $value; } 74 - $ }- return $b; 78}