月度归档:2014年01月

phpexcel Allowed memory size of load xlsx 报错解决

一句话:

$PHPReader->setReadDataOnly(true); //加上这个属性就OK    调试了一下午才发现这个

放个DEMO

    function  info(){
        //phpinfo();
        require_once realpath('.').'/application/third_party/PHPExcel/Classes/PHPExcel.php';
        require_once realpath('.').'/application/third_party/PHPExcel/Classes/PHPExcel/IOFactory.php';

        $filePath="/data/www/svn/phapb1a/public/aaaaner/2014-01-02/52c54056befcb.xlsx";
        $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
        $cacheSettings = array('memoryCacheSize'=>'256MB');
        PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);

        //$objPHPExcel = new PHPExcel();
        $PHPReader = PHPExcel_IOFactory::createReader('Excel2007');
        //$PHPReader = new PHPExcel_Reader_Excel2007();
        if(!$PHPReader->canRead($filePath)){
            $PHPReader = PHPExcel_IOFactory::createReader('Excel5');
            echo "2007";
            if(!$PHPReader->canRead($filePath)){
                echo 'no Excel';
                exit ;
            }
        }
        $PHPReader->setReadDataOnly(true);
        $objPHPExcel = $PHPReader->load($filePath);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestDataRow();
        $highestColumn = $sheet->getHighestDataColumn();
        //循环读取excel文件,读取一条,插入一条

        for($j=2;$j<=$highestRow;$j++){
            $i=0;
            $data=array();
            for($k='A';$k<=$highestColumn;$k++){
                $tmp=$sheet->getCell($k.$j)->getValue();
                if($tmp instanceof PHPExcel_RichText){
                    $tmp_str=$tmp->__toString();
                }else{
                    $tmp_str=$tmp;
                }
                var_dump($tmp_str);
                $tmp_str=get_encoding($tmp_str,'UTF-8');
                switch ($i){
                    case 0:
                        $data['bd_num_name']=$tmp_str;
                        break;
                    case 1:
                        $data['room_no']=$tmp_str;
                        break;
                    case 2:
                        $data['username']=$tmp_str;
                        break;
                    case 3:
                        $data['tel']=$tmp_str;
                        if(!check_ismobile($data['tel'])){
                            continue 3; //不是手机号的话
                        }
                        break;
                    case 4:
                        $data['types']=$tmp_str;
                        break;
                    case 5:
                        $data['unit_name']=$tmp_str;
                        break;
                    case 6:
                        $data['province']=$tmp_str;
                        break;
                    case 7:
                        $data['city']=$tmp_str;
                        break;
                    case 8:
                        $data['area']=$tmp_str;
                        break;
                    case 9:
                        $data['all_address']=$tmp_str;
                        break;
                    case 10:
                        $data['note']=$tmp_str;
                        break;
                }
                $i=$i+1;
            }
            $data=array_filter($data);
            var_dump($data);
    }
    }