I previously wrote a PHP read the contents of a CSV file
On the Code index.php
<?PHP/** * * @author XC **/ classexcel{ Public$currentSheet; Public$filePath; Public$fileType; Public$sheetIndex =0; Public$allColumn; Public$allRow; Publicfunction Initialized ($filePath) {if(File_exists ($filePath)) {$ This->filepath=$filePath; }Else{ returnArray (); } //cache as hard disk$cacheMethod =phpexcel_cachedobjectstoragefactory:: Cache_to_discisam; $cacheSettings=Array (); Phpexcel_settings::setcachestoragemethod ($cacheMethod, $cacheSettings); $file _ext=strtolower (PathInfo ($ This-FilePath, pathinfo_extension)); Switch($file _ext) { Case 'CSV': $ This->filetype='CSV'; Break; Case 'xlsx': $ This->filetype='Excel'; Break; Case 'xls': $ This->filetype='Excel'; Break; default: $ This->filetype="'; Break; } if($ This->filetype=='CSV') {$PHPReader=Newphpexcel_reader_csv (); //Default Character Set$PHPReader->setinputencoding ('GBK'); //Default delimiter$PHPReader->setdelimiter (','); if(! $PHPReader->canread ($ This-FilePath)) { returnArray (); }}elseif ($ This->filetype=='Excel') {$PHPReader=Newphpexcel_reader_excel2007 (); if(! $PHPReader->canread ($ This-FilePath)) {$PHPReader=NewPhpexcel_reader_excel5 (); if(! $PHPReader->canread ($ This-FilePath)) { returnArray (); } } }Else{ returnArray (); } $PHPReader->setreaddataonly (true); $PHPExcel= $PHPReader->load ($ This-FilePath); $ This->currentsheet = $PHPExcel->getsheet ((int)$ This-Sheetindex); //$this->currentsheet = $PHPExcel->getactivesheet ();$ This->allcolumn=$ This->currentsheet->Gethighestcolumn (); $ This->allrow=$ This->currentsheet->Gethighestrow (); } Publicfunction fetch ($beginRow =null, $endRow =NULL) {$currentSheet=$ This-CurrentSheet; $allColumn=$ This->allcolumn; $allRow =$ This-Allrow; $DATASRC= $data =Array (); //Get column headers for($currentColumn ='A'; $currentColumn <= $allColumn; $currentColumn + +) {$val= $currentSheet->getcellbycolumnandrow (ord ($currentColumn)- $,1)->getvalue ();//Ord () turns characters into decimal numbers$DATASRC [Ord ($currentColumn)- $]=Strtolower (Trim ($val));} //echo implode ("\ T", $DATASRC);$beginRow = $beginRow? $beginRow:2; $endRow= $endRow?$endRow: $allRow; for($currentRow = $beginRow; $currentRow <= $endRow; $currentRow + +){ //start Output $datarow=array () from column A; for($currentColumn ='A'; $currentColumn <= $allColumn; $currentColumn + +) {$val= $currentSheet->getcellbycolumnandrow (ord ($currentColumn)- $, $currentRow)->getvalue ();//Ord () turns characters into decimal numbers$dataRow [$DATASRC [Ord ($currentColumn)- $]]=$val; //cell-level data processing ... formatting dates, etc. } //row-level data processing ... if($dataRow) {$data []=$dataRow;} } //Echo ' <pre> ', Print_r ($data), ' </pre> '; //echo "\ n"; return$data; }}require_once'classes/phpexcel.php'; $import=NewExcel (); $import->initialized (DirName (__file__).'/test.xlsx'); Header ("content-type:text/html; Charset=utf-8"); Echo'<pre>', Print_r ($import->fetch ()),'</pre>';?>
One of the $import->initialized (dirname (__file__). '/test.xlsx'); test.xlsx Modify the execl path to its own
Require_once 'classes/phpexcel.php'; Modify the path to your own phpexcel.php
phpexcel.php can be downloaded from the official website http://phpexcel.codeplex.com/
Phpexcel read the XLS xlsx CSV format for Excel