How PHP uses Phpexcel to import exported data _php tips

Source: Internet
Author: User
Tags php programming set time

The example in this article describes how PHP uses Phpexcel to import exported data. Share to everyone for your reference, specific as follows:

Import data:

<?php error_reporting (E_all); Open error Set_time_limit (0); Script does not timeout Date_default_timezone_set (' Europe/london '); Set time/** Include path **/set_include_path (Get_include_path (). Path_separator.
' http://www.jb51.net/../Classes/')/SET environment variable/** phpexcel_iofactory * * include ' phpexcel/iofactory.php '; $inputFileType = ' Excel5 ';
This is read xls $inputFileType = ' 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, ' <br/> ';
  $objReader = Phpexcel_iofactory::createreader ($inputFileType);
  $objPHPExcel = $objReader->load ($inputFileName);
  /* $sheet = $objPHPExcel->getsheet (0); $highestRow = $sheet->gethighestrow (); Get the total number $highestColumn = $sheet->gethighestcolumn (); Get the total column/$objWorksheet = $objPHPExcel->getactivesheet ()//$highestRow = $objWorksheet->gethigHestrow ()///Get total number of columns echo ' highestrow= '. $highestRow;
  echo "<br>";
  $highestColumn = $objWorksheet->gethighestcolumn (); $highestColumnIndex = phpexcel_cell::columnindexfromstring ($highestColumn);//Total number of columns echo ' highestcolumnindex= '. $
  Highestcolumnindex;
  echo "<br/>";
  $headtitle =array ();
   for ($row = 1; $row <= $highestRow; $row + +) {$strs =array (); Note The index of the number of Highestcolumnindex columns starts with 0 for ($col = 0; $col < $highestColumnIndex; $col + +) {$strs [$col] = $objWorksheet
   ->getcellbycolumnandrow ($col, $row)->getvalue (); $info = Array (' word1 ' => "$strs [0]", ' Word2 ' => "$strs [1]", ' word3 ' => ' $strs [2] ", ' Word4 ' =& gt; "
    $strs [3] ",);
    Here, you can connect to your database, write to the database Print_r ($info);
  echo ' <br/> '; }?>

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 timeout date_defaul T_timezone_set (' Europe/london '); Set 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")->setlastmodifiedb Y ("Maarten Balliauw")->settitle ("Office 2007 XLSX Test Document")->setsubject ("Office 2007 XLSX Test Docum
    Ent ")->setdescription (" Test document for Office 2007 XLSX, generated using PHP classes. ")
 ->setkeywords ("Office 2007 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-officedocu
 Ment.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 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 will help you with your PHP programming.

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.