Import
Public Function Excel_put () {//First file upload, save file $path =$_files[' file '); $filePath = "uploads/". $path ["name"]; Move_uploaded_file ($path ["Tmp_name"], $filePath); By default read Excel with excel2007, if the format is not correct, then use the previous version of the Read//table field name $data =array (' B ' = ' = ' name ', ' C ' = ' pwd ', ' D ' = = ' money 1 ', ' E ' = ' salt '); $tablename = ' user1 ';//table name $this->excel_fileput ($filePath, $data, $tablename); } Private Function Excel_fileput ($filePath, $data, $tablename) {$this->load->library ("Phpexcel");//ci frame Introduction of 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 from column A Out *///echo $allColumn; for ($currentColumn = ' A '; $currentColumn <= $allColumn; $currentColumn + +) {$val = $currentSheet->get Cellbycolumnandrow (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 "
"; }//echo "\ n"; echo "Import succeeded"; }
Export
Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:attachment;filename=123.xls"); $array = $this->db->get ("shop_address")->result_array (); $str = "id\tname\tpid\n"; foreach ($array as $val) { $str. = $val [' id ']. "\ t". $val [' name ']. "\ T". $val [' pid ']. "\ n"; } Echo $str;
The above describes the import and export of Excel, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.