Import and Export of Excel

Source: Internet
Author: User
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.

  • 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.