PHP Excel read Excel file conversion array

Source: Internet
Author: User
Tags php excel

/*
Remark: Download PHP excel--http://download-codeplex.sec.s-msft.com/download/release first? projectname=phpexcel&downloadid=809023&filetime=130382506270130000&build=20907
*/

/** Convert Excel to an array by aibhsc**/require (Root_path.'includes/phpexcel.php');//Introducing the PHP Excel classfunction Format_excel2array ($filePath ="', $sheet =0){ if(Empty ($filePath) or!file_exists ($filePath)) {Die ('File NOT exists');} $PHPReader=NewPhpexcel_reader_excel2007 ();//Create a Reader object if(! $PHPReaderCanRead ($filePath)) {$PHPReader=NewPhpexcel_reader_excel5 (); if(! $PHPReaderCanRead ($filePath)) {echo'No Excel'; return ; }} $PHPExcel= $PHPReader->load ($filePath);//Create an Excel object$currentSheet = $PHPExcel->getsheet ($sheet);//* * Read the specified worksheet in the Excel file * /$allColumn = $currentSheet->gethighestcolumn ();//* * Get the largest column number */$allRow = $currentSheet->gethighestrow ();//* * Get a total number of lines * /$data =Array (); for($rowIndex =1; $rowIndex <= $allRow; $rowIndex + +) {//iterates through the contents of each cell. Note that the row starts at 1 and the column starts from a for($colIndex ='A'; $colIndex <= $allColumn; $colIndex + +) {$addr=$colIndex. $rowIndex; $cell= $currentSheet->getcell ($addr)GetValue (); if($cell instanceof Phpexcel_richtext) {//Rich Text conversion string$cell = $cell__tostring (); } $data [$rowIndex] [$colIndex]=$cell; } } return$data;} How to use: $filePath= Root_path.'data/diamondstock.xlsx';//Diamond Stock Documents$data =Format_excel2array ($filePath); Print_r ($data);d ie; Example output: Array ([1] =Array ([A]=Product code [B]=product name [C]=total weight [D]=purchase price [E]=sales Price [F]=4C notes) [2] =Array ([A]=10001[B]= gia-2156685995[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [3] =Array ([A]=10002[B]= gia-2156685996[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [4] =Array ([A]=10003[B]= gia-2156685997[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [5] =Array ([A]=10004[B]= gia-2156685998[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [6] =Array ([A]=10005[B]= gia-2156685999[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [7] =Array ([A]=10006[B]= gia-2156686000[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [8] =Array ([A]=10007[B]= gia-2156686001[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [9] =Array ([A]=10008[B]= gia-2156686002[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [Ten] =Array ([A]=10009[B]= gia-2156686003[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [ One] =Array ([A]=10010[B]= gia-2156686004[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [ A] =Array ([A]=10011[B]= gia-2156686005[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [ -] =Array ([A]=10012[B]= gia-2156686006[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR) [ -] =Array ([A]=10013[B]= gia-2156686007[C]=0.7[D]=1760[E]=1848[F]= g color,0. 7ct, SI1, FR))

PHP Excel read Excel file conversion array

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.