PHPExcel
程序员文章站
2022-07-07 09:02:57
1 /** 2 * 读取excel 3 */ 4 public function index() 5 { 6 $fileName = 'USCA'; 7 $fileContent = ''; 8 ini_set('memory_limit','3072M'); // 临时设置最大内存占用为3G 9 ... ......
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 }