Phpexcel read the XLS xlsx CSV format for Excel

Source: Internet
Author: User
Tags ord

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

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.