今天就跟大家聊聊有關(guān)使用php怎么上傳excel表格并獲取數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
一:首先是html部分
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file" /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>
二:就是去接收和處理上傳的文件了。php部分
//文件存放的路徑 $save_path = "/upload/"; //文件存放的文件夾 $save_files = $this->geturl(); 這個(gè)是以年月日新建的文件夾,僅供參考。 //先檢查當(dāng)前文件夾是否存在,如不存在,創(chuàng)建文件夾 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))//判斷文件夾是否存在 { mkdir($path); } //return $path."/"; return $str."/"; } //這個(gè)是上傳文件到需要保存的位置, if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_path)) { $error = "error|上傳文件錯(cuò)誤."; exit(0); }
下面開(kāi)始獲取你上傳的excel數(shù)據(jù)了
//獲取上傳表格的數(shù)據(jù) $file_name = $save_path.$file_path; //獲取上傳文件的地址名稱(chēng) 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'); //建立reader對(duì)象 $objPHPExcel = $objReader->load($file_name); $sheet = $objPHPExcel->getSheet(); $highestRow = $sheet->getHighestDataRow(); // 取得總行數(shù) $highestColumn_num = PHPExcel_Cell::columnIndexFromString($sheet->getHighestDataColumn()); //列數(shù) //$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++) { //讀取單元格 $value = $objPHPExcel->getActiveSheet()->getCell($columns[$k] . $j)->getValue();//這個(gè)就是獲取每個(gè)單元格的值 $value = trim($value); if (empty($value)) { $value = NULL; } $dealer_element[$k] = $value; //這里可以根據(jù)要求,做一些數(shù)據(jù)的驗(yàn)證 } $arr_result[$j] = $dealer_element; } echo json_encode($arr_result);
看完上述內(nèi)容,你們對(duì)使用php怎么上傳excel表格并獲取數(shù)據(jù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站題目:使用php怎么上傳excel表格并獲取數(shù)據(jù)-創(chuàng)新互聯(lián)
URL標(biāo)題:http://aaarwkj.com/article4/dpeoie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站排名、App開(kāi)發(fā)、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容