This article mainly introduced the CI Framework (CodeIgniter) Implementation of the import, export data operations, combined with an example of the CodeIgniter framework Libraies reference PHPEXCEL implementation of data import and export related operation skills, the need for friends can refer to the following
This article describes the import and export data operations implemented by the CI Framework (codeigniter). Share to everyone for your reference, as follows:
Referencing the Phpexcel class in Libraies (phpexcel.php)
Public Function Excel_put () { //First file upload, save file $path =$_files[' file '); $filePath = "uploads/". $path ["name"]; Move_uploaded_file ($path ["Tmp_name"], $filePath); $data =array (' B ' + = ' name ', ' C ' = ' pwd ', ' D ' = ' money1 ', ' E ' = ' salt '); $data =array (' B ' + = ' name ', ' C ' = ' pid '); $tablename = ' city2 ';//table name $this->excel_fileput ($filePath, $data, $tablename);}
Private Function Excel_fileput ($filePath, $data, $tablename) {$this->load->library ("Phpexcel");// The CI framework introduces the Excel class $PHPExcel = new Phpexcel (); $PHPReader = new phpexcel_reader_excel2007 (); if (! $PHPReader->canread ($filePath)) {$PHPReader = new Phpexcel_reader_excel5 (); if (! $PHPReader->canread ($filePath)) {echo ' no Excel '; return; }}//load Excel file $PHPExcel = $PHPReader->load ($filePath); Reads the first worksheet in an Excel file $currentSheet = $PHPExcel->getsheet (0); Get the maximum column number $allColumn = $currentSheet->gethighestcolumn (); How many lines are obtained $allRow = $currentSheet->gethighestrow (); The output starts from the second line, because the first behavior in the Excel table is column name for ($currentRow = 2; $currentRow <= $allRow; $currentRow + +) {/** starts output from column A///echo $all Column; for ($currentColumn = ' A '; $currentColumn <= $allColumn; $currentColumn + +) {$val = $currentSheet->getcellbycolumna Ndrow (Ord ($currentColumn)-$currentRow)->getvalue (); Print_r ($val); Die if ($currentColumn = = ' A ') { echo $val. " \ t "; }else if ($currentColumn <= $allColumn) {$data 1[$currentColumn]= $val; }} foreach ($data as $key = + $val) {$data 2[$val]= $data 1[$key]; } $this->db->insert ($tablename, $data 2); Print_r ($data 2); echo "</br>"; }//echo "\ n"; echo "Import succeeded";}
Export Data:
Public Function Excel_out () { header ("content-type:text/html"); Header ("Content-disposition:attachment;filename=123.xls"); $array = $this->db->get ("City")->result_array (); $str = "Id\t". " Name\t "." pid\n "; foreach ($array as $val) { $str. = $val [' id ']. " \ t ". $val [' name ']." \ t ". $val [' pid ']." \ n "; } echo $str;}
Articles you may be interested in:
Small program face recognition with PHP face login function
Example of MySQL database backup function implemented by thinkphp framework
PHP implementation prevents form repeat submission function "Token-based verification"