php excel导入功能

个人总结的php excel导入功能

贴出主要代码:

/**
 * @Author: 小枫哥
 * @Date: 2018-12-21 10:40:10
 * @Param: $HighestColumn |列数; $HighestRow |行数; $sheettmp |当前活动sheet对象
 * @Last Modified time: 2018-12-21 14:41:10
 * @notice:此方法只适用于phpexcel搭配使用
 */
    static  function importExcel($tmp_file,$name,$size){
        include_once ROOT_PATH.'phpexcel/PHPExcel.php';
        // 判断Excel文件版本类型
        $excelType = PHPExcel_IOFactory::identify( $tmp_file );
        if ($excelType == 'Excel5'){
            include_once ROOT_PATH.'PHPexcel/PHPExcel/Reader/Excel5.php';
            include_once ROOT_PATH.'phpexcel/PHPExcel/Writer/Excel5.php';
        }else if ($excelType == 'Excel2007')
        {
            include_once ROOT_PATH.'PHPexcel/PHPExcel/Reader/Excel2007.php';
            include_once ROOT_PATH.'phpexcel/PHPExcel/Writer/Excel2007.php';
        }
        // 设置PHPExcel缓存机制
        $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_wincache;
        $cacheSettings = array( 'cacheTime'  => 600 );
        PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
        //创建一个excel对象和一个reader
        $objPHPExcel = new PHPExcel();
        $objReader = PHPExcel_IOFactory::createReader ( $excelType );
        //读取Excel模板
        $objPHPExcel = $objReader->load ( $tmp_file );
        $objPHPExcel->setActiveSheetIndex(0);
        $sheettmp = $objPHPExcel->getActiveSheet();
        $HighestRow  = $sheettmp->getHighestColumn(); // 取得总列数
        $HighestColumn = $sheettmp->getHighestRow();    // 取得行数
            //excel的数据组装成数组
            $excelArr=self::excel_toArr($start,$HighestColumn,$HighestRow, $sheettmp);
           
        }

    static function excel_toArr($start,$HighestColumn,$HighestRow, $sheettmp){
        $excellie = array();
        $excelArr = array();
        $index = 0;
        //注意excel中Z之后就是AA,AB,AC,此方法适用与一般的导入
        $character = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        $key=array_search($HighestRow,$character);
        for ($i=0; $i < 26; $i++) { 
           if($i < $key+1 ){
                $excellie[] = $character[$i];
           }else{
                break;
           }
        }
        for ($i = $start; $i < $HighestColumn+1; $i++){
            $tmp=array();
            for($k=0;$k<count($excellie); $k++){
                $cell = $sheettmp->getCell($excellie[$k].$i)->getCalculatedValue();
                if($cell instanceof PHPExcel_RichText){ //富文本转换字符串
                    $cell = $cell->__toString();
                }
              $tmp[$excellie[$k].$i] = $cell;

            }
            $excelArr[$index] = $tmp;
            $index++;     
        }
        return $excelArr;
    }

    //获取到的数据形式如下
      array (
        0 => 
            array (
              'A2' => 12,
              'B2' => 222,
              'C2' => 22,
              'D2' => 2,
              'E2' => 2
            ),
        1 => 
            array (
              'A3' =>12,
              'B3' =>222,
              'C3' => 22,
              'D3' => 2,
              'E3' => 2
            ),
        2 => 
            array (
              'A4' =>  12,
              'B4' =>  3.3366,
              'C4' =>  22,
              'D4' =>  2,
              'E4' =>  2
            )
    );

猜你喜欢

转载自blog.csdn.net/qq_39586877/article/details/85160254