The previous blog post has done a brief summary of the Phpexcel export Excel file, and now he reads Excel to do the following summary. (The amount of data will not be very large can be used in the web to read directly with this method, if the amount of data will be very large, it is recommended that the Web only upload function, read, processing or put the backstage bar. Phpexcel is still more time-consuming and memory-intensive. )
Instance code:
//first import phpexcelrequire_once ' phpexcel.php '; $filePath = "test.xlsx";// Create Reader Object $phpreader = new phpexcel_reader_excel2007 (); if (! $PHPReader->canread ($filePath)) {$PHPReader = new Phpexcel_reader_excel5 (); if (! $PHPReader->canread ($filePath)) {echo ' no Excel '; return; }}//Create an Excel object, you can read the file through an Excel object, or write it to a file $phpexcel = $PHPReader->load ($filePath);/** read the first worksheet in the Excel file */$ CurrentSheet = $PHPExcel->getsheet (0);/** gets the largest column number */$allColumn = $currentSheet->gethighestcolumn ();/** get a total of how many lines */$allRow = $currentSheet->gethighestrow ();//Cycle through the contents of each cell. Note that the row starts at 1, and the column starts with A for ($rowIndex =1; $rowIndex <= $allRow; $rowIndex + +) {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 (); Echo $cell; }}
What you need to illustrate here is the "Rich Text conversion string" in the comments above.
Phpexcel reads an Excel file, if the contents of a cell have two fonts, the object that is read is Rich text:
For example: The contents of the cell: "Test 1", where the first half of the "test" font for the song body, the second half of the "1" font is Calibri, this time through
$cell = $sheet->getcell ($addr)->getvalue ();
Gets the value of the cell. and print:
Phpexcel_richtext Object ([_richtextelements:private] = = Array ([0] = Phpexcel_richtext_textelement objec T ([_text:private] + = Test) [1] = Phpexcel_richtext_run Object ( [_font:private] = Phpexcel_style_font Object ([_ Name:private] = Calibri [_size:private] = 11 [_bold:private] [_italic:private] = = [_superscript:private] [_subscript:private] = = [_underline:private] = none [_strikethrough:private] = = [_color:private] = Phpexcel_style_color Object ( [_argb:private] = FF000000 [_issupervis Or:private] = [_parent:private] = [_parentpropertyname:private] (=) [_parentpropertyname:private] [_issupervisor:private] = = [_parent:private] [ColorIndex] = 8 ) [_text:private] = 1) ) )
You can see that the text contents of the cell cannot be read directly to such a cell. (Note: The rich text here is my own translation, do not know to no).
In addition, the functions of reading cells are:
Column starting from 0, line starting from 1
$currentSheet->getcellbycolumnandrow ($colIndex, $rowIndex)->getvalue ();