PHP Import Export Data using Phpexcel method, Phpexcel Import Export _php Tutorial

Source: Internet
Author: User

PHP uses Phpexcel to import and export data, phpexcel Import and Export


This example describes how PHP imports and exports data using Phpexcel. Share to everyone for your reference, as follows:

Import data:

<?phperror_reporting (E_all); Turn On Error set_time_limit (0); The script does not time out date_default_timezone_set (' Europe/london '); Set the time/** Include path **/set_include_path (Get_include_path (). Path_separator. ' http://www.bkjia.com/. /classes/');//Set environment variable/** phpexcel_iofactory */include ' phpexcel/iofactory.php ';//$inputFileType = ' Excel5 '; This is the $inputFileType to read the XLS = ' Excel2007 ';//This is the xlsx//$inputFileName = './sampledata/example2.xls '; $inputFileName = '.  /sampledata/book.xlsx '; Echo ' Loading file ', PathInfo ($inputFileName, pathinfo_basename), ' using iofactory with a defined reader type of ', $inputFi Letype, '
'; $objReader = Phpexcel_iofactory::createreader ($inputFileType); $objPHPExcel = $objReader->load ($inputFileName); /* $sheet = $objPHPExcel->getsheet (0); $highestRow = $sheet->gethighestrow (); Total number of rows obtained $highestColumn = $sheet->gethighestcolumn (); Get the Total column */$objWorksheet = $objPHPExcel->getactivesheet ();//Get head count $highestRow = $objWorksheet->gethighestrow (); /Get total number of columns echo ' highestrow= '. $highestRow; echo "
"; $highestColumn = $objWorksheet->gethighestcolumn (); $highestColumnIndex = phpexcel_cell::columnindexfromstring ($highestColumn);//Total number of columns echo ' highestcolumnindex= '. $ Highestcolumnindex; echo "
"; $headtitle =array (); for ($row = 1; $row <= $highestRow; $row + +) {$strs =array (); Note that the number of columns in the Highestcolumnindex index starts at 0 for ($col = 0; $col < $highestColumnIndex; $col + +) {$strs [$col] = $objWorksheet-&G T;getcellbycolumnandrow ($col, $row)->getvalue (); } $info = Array (' word1 ' = ' + ' $strs [0] ', ' word2 ' = ' $strs [1] ', ' word3 ' = ' $strs ' [2] ', ' word4 ' = ' $ STRS[3] ",); Here, you can connect to your database, write to the database Print_r ($info); Echo '
'; }?>

Export Data:

(If there is a special string = Trouble Str_replace (Array (' = '), ', $val [' roleName ']);)

Private Function _export_data ($data = Array ()) {error_reporting (E_all);//Open Error Set_time_limit (0);//script does not time out Date_default_ Timezone_set (' Europe/london '); Set the time/** Include path **/set_include_path (Fcpath. APPPATH. ' /libraries/classes/')//SET environment variable//Create new Phpexcel object Include ' phpexcel.php '; $objPHPExcel = new Phpexcel (); Set Document Properties $objPHPExcel->getproperties ()->setcreator ("Maarten Balliauw") Setlastmodifiedby ("Maarten Balliauw")->settitle ("Office" XLSX Test Document ")->setsubject (" Office X    LSX Test Document ")->setdescription (" Test document for Office-XLSX, generated using PHP classes. ") ->setkeywords ("Office openxml PHP")->setcategory ("Test result file"); Add some data $letter = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U '     , ' V ', ' W ', ' X ', ' Y ', ' Z ');  if ($data) {$i = 1; foreach ($data as $key = = $value) {$newobj = $objPHPExcel->setactivesheetindeX (0);   $j = 0; foreach ($value as $k = = $val) {$index = $letter [$j]. "   $i ";   $objPHPExcel->setactivesheetindex (0)->setcellvalue ($index, $val);  $j + +;  } $i + +;   }} $date = Date (' y-m-d ', Time ()); Rename worksheet $objPHPExcel->getactivesheet ()->settitle ($date); $objPHPExcel->setactivesheetindex (0); Redirect output to a client ' s Web browser (Excel2007) header (' content-type:application/ Vnd.openxmlformats-officedocument.spreadsheetml.sheet '); Header (' Content-disposition:attachment;filename= '. $date. '. Xlsx "'); Header (' cache-control:max-age=0 '); $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); $objWriter->save (' php://output '); Exit;}

Directly on the code:

Public Function Export_data ($data = Array ()) {# code ... include_once (app_path. ' Tools/phpexcel/classes/phpexcel/writer /iwriter.php '); Include_once (app_path. ' tools/phpexcel/classes/phpexcel/writer/excel5.php '); Include_once (app_path. ' tools/phpexcel/classes/phpexcel.php '); Include_once (app_path. ' tools/phpexcel/classes/phpexcel/iofactory.php '); $obj _phpexcel = new Phpexcel (); $obj _phpexcel->getactivesheet ()->setcellvalue (' A1 ', ' Key ');  $obj _phpexcel->getactivesheet ()->setcellvalue (' B1 ', ' Value ');  if ($data) {$i = 2;  foreach ($data as $key = $value) {# code ... $obj _phpexcel->getactivesheet ()->setcellvalue (' a '. $i, $value);  $i + +; }} $obj _writer = Phpexcel_iofactory::createwriter ($obj _phpexcel, ' Excel5 '); $filename = "Outexcel.xls";  Header ("Content-type:application/force-download");  Header ("Content-type:application/octet-stream");  Header ("Content-type:application/download");  Header (' Content-disposition:inline;filename= '. $filename. ' '); Header ("Content-transFer-encoding:binary "); Header ("last-modified:".) Gmdate ("D, D M Y h:i:s").  "GMT");  Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");  Header ("Pragma:no-cache"); $obj _writer->save (' php://output '); }

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1071394.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071394.html techarticle PHP uses Phpexcel Import Export data method, Phpexcel import and Export This article describes how PHP uses Phpexcel import and export data. Share to everyone for your reference, as follows: ...

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