1 /** 2 * 讀取excel 3 */ 4 public function index() 5 { 6 $fileName = 'USCA'; 7 $fileContent = ''; 8 ini_set('memory_limit','3072M'); // 臨時設置最大內存佔用爲3G 9 set_time_limit(0); // 設置腳本最大執行時間 爲0 永不過時 10 require __DIR__ . '/../Common/Excel/PHPExcel/IOFactory.php'; 11 $uploadfile = "D:/tel/$fileName.xlsx"; 12 $reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 讀取 excel 文檔 13 $PHPExcel = $reader->load($uploadfile); // 文檔名稱 14 $sheet = $PHPExcel->getSheet(0); 15 $highestRow = $sheet->getHighestRow(); // 取得總行數 16 $highestColumn = $sheet->getHighestColumn(); // 取得總列數 17 18 // 獲取一行的數據 19 for ($row = 1; $row <= $highestRow; $row++){ 20 // Read a row of data into an array 21 $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); 22 if (count($rowData) >0) { 23 $rowData = $rowData[0]; 24 foreach ($rowData as $key => $value) { 25 $str = preg_replace('/[^0-9]+/','',$value); 26 $str = trim($str); 27 if ( strlen( $str) > 5) { 28 // $arr = explode("-",$str); 29 // if (count($arr) > 1) { 30 // $str = preg_replace('/[^0-9]+/','',$arr[0]); 31 // echo "INSERT into t_au(tel) VALUE($str);"; 32 // $str = preg_replace('/[^0-9]+/','',$arr[1]); 33 // echo "INSERT into t_au(tel) VALUE($str);"; 34 // }else{ 35 $fileContent .= "INSERT into t_$fileName(tel) VALUE($str);"; 36 // } 37 } 38 } 39 } 40 } 41 self::addFile($fileName,$fileContent); 42 } 43 44 public function addFile($fileName,$content) 45 { 46 47 $myfile = fopen("d:/tel/$fileName.sql", "a") or die("Unable to open file!");//這個是在c盤根目錄生成文件 48 fwrite($myfile, $content);//寫入內容,能夠寫屢次哦,不過沒啥意義,由於你拼接好字符串,一次寫入就好了 49 fclose($myfile);//關閉該操做 50 }