yii中 集成PHPExcel 实现excel导入

  1. 下载PHPexcel    http://phpexcel.codeplex.com/releases/view/96183
  2. 下载完成后将其解压至extensions/yiiexcel下 yiiexcel是后来创建的文件夹
  3. 修改PHPExcel/Autoloader.php文件 将PHPExcel/Autoloader.php文件修改为
    <?php
    /**
     * PHPExcel
     *
     * Copyright (c) 2006 - 2012 PHPExcel
     *
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
     * version 2.1 of the License, or (at your option) any later version.
     *
     * This library is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
     *
     * You should have received a copy of the GNU Lesser General Public
     * License along with this library; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     *
     * @category   PHPExcel
     * @package    PHPExcel
     * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
     * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
     * @version    1.7.8, 2012-10-12
     */
    
    PHPExcel_Autoloader::Register();
    //	As we always try to run the autoloader before anything else, we can use it to do a few
    //		simple checks and initialisations
    PHPExcel_Shared_ZipStreamWrapper::register();
    // check mbstring.func_overload
    if (ini_get('mbstring.func_overload') & 2) {
        throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
    }
    PHPExcel_Shared_String::buildCharacterSets();
    
    
    /**
     * PHPExcel_Autoloader
     *
     * @category	PHPExcel
     * @package		PHPExcel
     * @copyright	Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
     */
    class PHPExcel_Autoloader
    {
    	/**
    	 * Register the Autoloader with SPL
    	 *
    	 */
    	public static function Register() {
    		/*if (function_exists('__autoload')) {
    			//	Register any existing autoloader function with SPL, so we don't get any clashes
    			spl_autoload_register('__autoload');
    		}
    		//	Register ourselves with SPL
    		return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));
    		 */
    		$functions = spl_autoload_functions();
    		foreach ( $functions as  $function)
    		spl_autoload_unregister($function);
    		$functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions);
    		foreach ( $functions as $function)
    		$x = spl_autoload_register($function);
    		return $x;
    	}	//	function Register()
    
    
    	/**
    	 * Autoload a class identified by name
    	 *
    	 * @param	string	$pClassName		Name of the object to load
    	 */
    	public static function Load($pClassName){
    		if ((class_exists($pClassName,FALSE)) || (strpos($pClassName, 'PHPExcel') !== 0)) {
    			//	Either already loaded, or not a PHPExcel class request
    			return FALSE;
    		}
    
    		$pClassFilePath = PHPEXCEL_ROOT .
    						  str_replace('_',DIRECTORY_SEPARATOR,$pClassName) .
    						  '.php';
    
    		if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
    			//	Can't load
    			return FALSE;
    		}
    
    		require($pClassFilePath);
    	}	//	function Load()
    
    }
  4. 在控制器的方法中写入代码
		Yii::import('ext.yiiexcel.PHPExcel',true);
		Yii::import('ext.yiiexcel.PHPExcel.IOFactory',true);
		Yii::import('ext.yiiexcel.PHPExcel.Reader.Excel5',true);
		$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
		
		$objPHPExcel = $objReader->load('sensitive.xls'); //$filename可以是上传的文件,或者是指定的文件
		$sheet = $objPHPExcel->getSheet(0); 
		$highestRow = $sheet->getHighestRow(); // 取得总行数 
		$highestColumn = $sheet->getHighestColumn(); // 取得总列数



		//循环读取excel文件,读取一条,插入一条
		//j表示从哪一行开始读取
		
	
		for($j=2;$j<=$highestRow;$j++)
		{
			$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A列的值
			$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B列的值
			$c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//获取C列的值
			echo $a.'---'.$b.'---'.$c.'<br />';
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值