PHP uses PHPexcel to import and export data. phpexcel import and export _ PHP Tutorial

Source: Internet
Author: User
PHP uses PHPexcel to import and export data, and phpexcel to import and export data. PHP uses PHPexcel to import and export data. phpexcel to import and export this article describes how PHP uses PHPexcel to import and export data. For your reference, see PHPexcel for how to import and export data.

This document describes how to use PHPexcel to import and export data in PHP. We will share this with you for your reference. The details are as follows:

Import data:

<? Phperror_reporting (E_ALL); // The error set_time_limit (0) is enabled; // 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 the environment variable/** PHPExcel_IOFactory */include 'phpexcel/IOFactory. php '; // $ inputFileType = 'excel5'; // This is the $ inputFileType = 'excel2007' for reading xls; // 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', $ inputFileType ,'
'; $ ObjReader = PHPExcel_IOFactory: createReader ($ inputFileType); $ objPHPExcel = $ objReader-> load ($ inputFileName ); /* $ sheet = $ objPHPExcel-> getSheet (0); $ highestRow = $ sheet-> getHighestRow (); // get the total number of rows $ highestColumn = $ sheet-> getHighestColumn (); // get the total number of columns */$ objWorksheet = $ objPHPExcel-> getActiveSheet (); // obtain the total number of rows $ highestRow = $ objWorksheet-> getHighestRow (); // obtain the total number of columns echo 'highestrow = '. $ highestRow; echo"
"; $ HighestColumn = $ objWorksheet-> getHighestColumn (); $ highestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn"
"; $ Headtitle = array (); for ($ row = 1; $ row <= $ highestRow; $ row ++) {$ strs = array (); // note that the index of the highestColumnIndex column starts from 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' => "$ strs [3]",); // you can connect here, your database is written into the database print_r ($ info); echo'
';}?>

Export data:

(If there are special strings = troublesome str_replace (array ('='), '', $ val ['rolename']);)

Private function _ export_data ($ data = array () {error_reporting (E_ALL); // The set_time_limit (0) error is enabled ); // script does not time out date_default_timezone_set ('Europe/London '); // Set the time/** Include path **/set_include_path (FCPATH. APPPATH. '/libraries/Classes/'); // sets the 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 2007 XLSX Test Document")-> setSubject ("Office 2007 XLSX Test Document ") -> 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-officedocument.spreadsheetml.sheet '); header ('content-Disposition: attachment; filename = "'.20.date.'.xlsx"'); header ('cache-Control: max-age = 0 '); $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); $ objWriter-> save ('php: // output'); exit ;}

Directly run 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 will help you with php programming.

Examples in this article describes how PHP uses PHPexcel to import and export data. For your reference, please refer to the following details :...

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.