Php uploads an excel file and obtains data. php uploads an excel file.
This is a feature that needs to be done recently. I have read a lot of related articles on the Internet, which is basically the same. I will sort it out here.
I. First, the html section
2: It is to receive and process uploaded files. Php Section
// File storage path $ save_path = "/upload/"; // file storage folder $ save_files = $ this-> geturl (); this is a folder created on the day of the year, month, and day, for reference only. // First check whether the current folder exists. If it does not exist, create the function geturl () {$ year = date ('y'); $ month = date ('M '); $ day = date ('D'); $ str = $ year. $ month. $ day; if (strtoupper (substr (PHP_ OS, 0, 3) = 'win') {$ path = getcwd (). "/upload /". $ str;} else {$ path = "/mnt/erp /". $ str;} if (! File_exists ($ path) // determines whether the folder exists {mkdir ($ path);} // return $ path. "/"; return $ str. "/";} // This is the location where the file to be saved is uploaded. if (! @ Move_uploaded_file ($ _ FILES [$ upload_name] ["tmp_name"], $ save_path. $ file_path) {$ error = "error | File Upload error. "; exit (0 );}
Next, we start to get the excel Data you uploaded.
// Obtain the data of the uploaded table $ file_name = $ save_path. $ file_path; // get the address name of the uploaded file require_once APPPATH. 'Views/IDC/config/PHPExcel. php '; require_once APPPATH. 'Views/IDC/config/PHPExcel/IOFactory. php '; require_once APPPATH. 'Views/IDC/config/PHPExcel/Cell. php '; $ objReader = PHPExcel_IOFactory: createReader ('excel2007'); // create a reader object $ objPHPExcel = $ objReader-> load ($ file_name ); $ sheet = $ objPHPExcel-> getSheet (); $ highestRow = $ sheet-> getHighestDataRow (); // get the total number of rows $ highestColumn_num = PHPExcel_Cell :: columnIndexFromString ($ sheet-> getHighestDataColumn (); // Number of columns // $ columns = PHPExcel_Cell: getColumn ($ highestColumn_num); $ columns = array ('A ', 'B', 'C', 'D', 'E', 'F', 'G'); $ arr_result = array (); $ dealer_element = array (); for ($ j = 2; $ j <= $ highestRow; $ j ++) {for ($ k = 0; $ k <count ($ columns ); $ k ++) {// read the cell $ value = $ objPHPExcel-> getActiveSheet ()-> getCell ($ columns [$ k]. $ j)-> getValue (); // This is to get the value of each cell $ value = trim ($ value); if (empty ($ value )) {$ value = NULL;} $ dealer_element [$ k] = $ value; // as required, perform some data verification} $ arr_result [$ j] = $ dealer_element;} echo json_encode ($ arr_result );
The above is all the content of this article. I hope the content of this article will help you in your study or work. At the same time, I also hope to provide more support to the customer's home.