Examples of import and export data operations implemented by the CI Framework (CodeIgniter)

Source: Internet
Author: User
Tags codeigniter
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"

Related Article

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.