PHP excel code for importing an excel file to a database
| In this article, PHP uses phpexcel to import database code. if you are interested in phpexcel, it is good to give a reference.
Phpexcel imports the content to the database. the code is as follows:
'; $ ObjReader = PHPExcel_IOFactory: createReader ($ inputFileType); $ objPHPExcel = $ objReader-> load ($ inputFileName ); /* $ sheet = $ objPHPExcel-> getSheet (0); $ highestRow = $ sheet-> getHighestRow (); // get the total number of rows $ highestColumn = $ sheet-> getHighestColumn (); // get the total number of columns */$ objWorksheet = $ objPHPExcel-> getActiveSheet (); // obtain the total number of rows $ highestRow = $ objWorksheet-> getHighestRow (); // obtain the total number of columns echo 'highestrow = '. $ highestRow; echo" "; $ HighestColumn = $ objWorksheet-> getHighestColumn (); $ highestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn" "; $ Headtitle = array (); for ($ row = 1; $ row <= $ highestRow; $ row ++) {$ strs = array (); // note that the index of the highestColumnIndex column starts from 0 for ($ col = 0; $ col <$ highestColumnIndex; $ col ++) {$ strs [$ col] = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row)-> getValue ();} $ info = array ('word1 '=> "$ strs [0]", 'word2' => "$ strs [1]", 'word3 '=> "$ strs [2]", 'word4' => "$ strs [3]",); // code can be placed in the reference, write data to the database print_r ($ info); echo' ';}?> |