Php code for reading excel files and converting them to arrays in PHPexcel
/** Convert the excel file to an array by aibscs **/require (ROOT_PATH. 'Des/PHPExcel. php '); // introduce the php excel class function format_excel2array ($ filePath = '', $ sheet = 0) {if (empty ($ filePath) or! File_exists ($ filePath) {die ('file not exists') ;}$ PHPReader = new PHPExcel_Reader_Excel2007 (); // Create a reader object if (! $ PHPReader-> canRead ($ filePath) {$ PHPReader = new PHPExcel_Reader_Excel5 (); if (! $ PHPReader-> canRead ($ 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 (); // ** obtain the maximum column number */$ allRow = $ currentSheet-> getHighestRow (); // ** obtain the total number of rows */$ data = array (); for ($ rowIndex = 1; $ rowIndex <= $ allRow; $ rowIndex ++) {// read the content of each cell cyclically. Note that the row starts from 1 and the column starts from A for ($ colIndex = 'a'; $ colIndexgetCell ($ addr)-> getValue (); if ($ cell instanceof PHPExcel_RichText) {// rich text conversion string $ cell = $ cell->__ toString ();} $ data [$ rowIndex] [$ colIndex] = $ cell ;}} return $ data;} usage: $ filePath = ROOT_PATH. 'data/diamondstock.xlsx'; // diamond inventory file $ data = format_excel2array ($ filePath); print_r ($ data); die; output result example: array ([1] => Array ([A] => Product No. [B] => Product name [C] => total weight [D] => purchase price [E] => Sales price [F] => 4C remarks) [2] => Array ([A] => 10001 [B] => GIA-2156685995 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [3] => Array ([A] => 10002 [B] => GIA-2156685996 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [4] => Array ([A] => 10003 [B] => GIA-2156685997 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [5] => Array ([A] => 10004 [B] => GIA-2156685998 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [6] => Array ([A] => 10005 [B] => GIA-2156685999 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [7] => Array ([A] => 10006 [B] => GIA-2156686000 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [8] => Array ([A] => 10007 [B] => GIA-2156686001 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [9] => Array ([A] => 10008 [B] => GIA-2156686002 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [10] => Array ([A] => 10009 [B] => GIA-2156686003 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [11] => Array ([A] => 10010 [B] => GIA-2156686004 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [12] => Array ([A] => 10011 [B] => GIA-2156686005 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [13] => Array ([A] => 10012 [B] => GIA-2156686006 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR) [14] => Array ([A] => 10013 [B] => GIA-2156686007 [C] => 0.7 [D] => 1760 [E] => 1848 [F] => G, 7CT, SI1, FR ))