<? If ($ _ POST ['leadexcel '] = "true ") { $ Filename = $ _ FILES ['input'] ['name']; $ Tmp_name = $ _ FILES ['input'] ['tmp _ name']; $ Msg = uploadFile ($ filename, $ tmp_name ); Echo $ msg; } // Import an Excel file Function uploadFile ($ file, $ filetempname) { // Set the path for storing uploaded files $ FilePath = 'upfile /'; $ Str = ""; // The following path is modified according to your PHPExcel path. Require_once '../PHPExcel. php '; Require_once '../PHPExcel/IOFactory. php '; Require_once '../PHPExcel/Reader/excel5.php '; // Set the time zone. $ Time = date ("y-m-d-H-I-s"); // The current upload time. // Obtain the extension of the uploaded file $ Extend = strrchr ($ file ,'.'); // Name of the uploaded file $ Name = $ time. $ extend; $ Uploadfile = $ filePath. $ name; // address of the uploaded file name // The move_uploaded_file () function moves the uploaded file to a new location. If yes, true is returned. Otherwise, false is returned. $ Result = move_uploaded_file ($ filetempname, $ uploadfile); // if the file is uploaded to the current directory // Echo $ result; If ($ result) // if the file is uploaded successfully, import the file to excel. { Include "conn. php "; $ ObjReader = PHPExcel_IOFactory: createReader ('excel5'); // use excel2007 for 2007 format $ ObjPHPExcel = $ objReader-> load ($ uploadfile ); $ Sheet = $ objPHPExcel-> getSheet (0 ); $ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows. $ HighestColumn = $ sheet-> getHighestColumn (); // gets the total number of Columns
/* Method 1 // Read the excel file cyclically, read one, and insert one For ($ j = 1; $ j <= $ highestRow; $ j ++) // read data from the first row { For ($ k = 'a'; $ k <= $ highestColumn; $ k ++) // read data from column { // This method is simple, but there is something wrong. Use ''to merge as an array and then split it into Field Values and insert them into the database. In excel, if the value of a cell contains imported data, it is null. // $ Str. = $ objPHPExcel-> getActiveSheet ()-> getCell ("$ k $ j")-> getValue (). ''; // read a cell } // Echo $ str; die (); // Explode: The function separates strings into arrays. $ Strs = explode ("", $ str ); $ SQL = "INSERT INTO te ('1', '2', '3', '4', '5') VALUES ( '{$ Strs [0]}', '{$ Strs [1]}', '{$ Strs [2]}', '{$ Strs [3]}', '{$ Strs [4]}') "; // Die ($ SQL ); If (! Mysql_query ($ SQL )) { Return false; Echo 'SQL statement error '; } $ Str = ""; } Unlink ($ uploadfile); // delete an uploaded excel File $ Msg = "Import successful! "; */ /* Method 2 */ $ ObjWorksheet = $ objPHPExcel-> getActiveSheet (); $ HighestRow = $ objWorksheet-> getHighestRow (); Echo 'hehestrow = '. $ highestRow; Echo "<br> "; $ HighestColumn = $ objWorksheet-> getHighestColumn (); $ HighestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn); // The total number of Columns Echo 'hehestcolumnindex = '. $ highestColumnIndex; Echo "<br> "; $ 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 (); } $ SQL = "INSERT INTO te ('1', '2', '3', '4', '5') VALUES ( '{$ Strs [0]}', '{$ Strs [1]}', '{$ Strs [2]}', '{$ Strs [3]}', '{$ Strs [4]}') "; // Die ($ SQL ); If (! Mysql_query ($ SQL )) { Return false; Echo 'SQL statement error '; } } } Else { $ Msg = "Import failed! "; } Return $ msg; } ?> |