//批量導入文章 excel讀取 public function importdata ( $filename, $tmp_name ) { //設置超時時間 set_time_limit(0); $rootPath = OA_FILES_ROOT.'/' ; //引入phpexcel // $_SERVER["WL_OA"] = 'E:/www/2.151/oa/web'; $_SERVER["WL_OA"] = '/opt/wwwroot/wl/oa/web'; require($_SERVER["WL_OA"] . "/Classes/PHPExcel.php"); require($_SERVER["WL_OA"] . "/Classes/PHPExcel/IOFactory.php"); require($_SERVER["WL_OA"] . "/Classes/PHPExcel/Reader/Excel5.php"); //判斷文件格式 $path_parts = pathinfo($filename) ; $extension = strtolower($path_parts['extension']) ; if ( !in_array($extension, array('xls','xlsx'))) { wl_set_operation_msg ( "文件格式不正確,請重新選擇", 2, bd_oa_url ( 'helpcenter/manage_article' ), 3 ); } //上傳后的文件名 $randName = substr(md5(rand(10,99)), 0,4) ; $name = date('YmdHis').$randName.'.'.$extension ; $realFile = $rootPath.$name; //上傳后的文件名地址 //移動上傳文件 $result = move_uploaded_file( $tmp_name, $realFile ); if ( $result ) { $PHPExcel = new PHPExcel() ; $PHPReader = new PHPExcel_Reader_Excel2007(); if( !$PHPReader->canRead($realFile) ){ $PHPReader = new PHPExcel_Reader_Excel5(); if( !$PHPReader->canRead( $realFile ) ){ wl_set_operation_msg ( "文件不可讀,請重新選擇", 2, bd_oa_url ( 'helpcenter/manage_article' ), 3 ); return false; } } $_excelData = array() ; //讀取excel $PHPExcel = $PHPReader->load( $realFile ); //獲取工作表的數目 $sheetCount = $PHPExcel->getSheetCount(); for ( $i = 0; $i < $sheetCount; $i++ ) { $_currentSheet = $PHPExcel->getSheet( $i ) ; $_allRow = $_currentSheet->getHighestRow(); //獲取Excel中信息的行數 $_allColumn = $_currentSheet->getHighestColumn();//獲取Excel的列數 $highestRow = intval( $_allRow ) ; $highestColumn = PHPExcel_Cell::columnIndexFromString($_allColumn);//有效總列數 for( $row = 2; $row <= $highestRow; $row++ ) { $_excelData['onecatalog'] = $_currentSheet->getCellByColumnAndRow(0, $row)->getValue() ; $_excelData['twocatalog'] = $_currentSheet->getCellByColumnAndRow(1, $row)->getValue() ; $_excelData['threecatalog'] = $_currentSheet->getCellByColumnAndRow(2, $row)->getValue() ; $_excelData['title'] = $_currentSheet->getCellByColumnAndRow(3, $row)->getValue() ; $_excelData['content'] = $_currentSheet->getCellByColumnAndRow(4, $row)->getValue() ; if ( empty($_excelData['onecatalog']) || empty($_excelData['twocatalog']) || empty($_excelData['threecatalog']) ) { self::add_log( array('sType'=>"導入失敗",'doContent'=>$filename." 下 Sheet".($i+1)." 中第 ".$row." 行導入失敗",'iUid'=>$this->loginedUid,'doTime'=>date("Y-m-d H:i:s"))) ; unset( $_excelData ) ; continue ; } if ( !is_string( $_excelData['title'] ) || !is_string( $_excelData['content'] ) || empty($_excelData['title']) || empty($_excelData['content']) ) { self::add_log( array('sType'=>"導入失敗",'doContent'=>$filename." 下 Sheet".($i+1)." 中第 ".$row." 行導入失敗",'iUid'=>$this->loginedUid,'doTime'=>date("Y-m-d H:i:s"))) ; unset( $_excelData ) ; continue ; }
//插入數據庫 if ( !self::doData( $_excelData ) ) { unset( $_excelData ) ; continue ; } unset($_excelData); } } return true ; }else{ wl_set_operation_msg ( "文件上傳失敗,請重新選擇", 2, bd_oa_url ( 'helpcenter/manage_article' ), 3 ); } }