博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHPExcel读取excel的多个sheet存入数据库
阅读量:6941 次
发布时间:2019-06-27

本文共 3681 字,大约阅读时间需要 12 分钟。

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

你可能感兴趣的文章
医保业务的相关概念
查看>>
【Mac使用系列】【转载】十几个Mac实用工具
查看>>
网易七鱼 Android 高性能日志写入方案
查看>>
微软Visual Studio 2010架构设计功能应用(转)
查看>>
干净的代码是改出来的
查看>>
微软面试题附答案(转)
查看>>
你必须要知道的架构知识~第三章 接口用来制定操作的统一性
查看>>
关于下拉菜单和iframe的问题
查看>>
ASP.NET File.Delete只读文件引起的访问被拒绝,设置文件属性为Normal
查看>>
Sharepoint学习笔记—ECMAScript对象模型系列-- 11、 Enable/Disable Ribbon上的Button
查看>>
python类库26[sqlite]
查看>>
苹果与三星的专利纠纷
查看>>
boost库在工作(36)网络服务端之六
查看>>
关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析
查看>>
CSS——float属性备忘笔记
查看>>
利用pushState开发无刷页面切换(转)
查看>>
(翻译)理解Java当中的回调机制
查看>>
Discuz! X 插件开发手册
查看>>
Spring 注解@Component,@Service,@Controller,@Repository
查看>>
让PHP7达到最高性能的几个Tips(转)
查看>>