PHPExcel读取excel的多个sheet存入数据库

//批量导入文章   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 ); } }

  

原文地址:https://www.cnblogs.com/shubuqi/p/3411079.html