关于PHPExcel直接读取Excel文件两种方法

//程序执行体
        set_time_limit(0); //设置页面等待时间
        ini_set('memory_limit', '-1');//不限制内存
        $dqml = dirname(__FILE__);          //获取当前文件地址
        require $dqml.'/PHPExcel/PHPExcel.php';  //引入文件
        $objPHPExcel = new PHPExcel();          //实例化PHPExcel类 相当于新建一个excel表格

        //查询最新文件名
        $sql = "SELECT * FROM `fielup` WHERE 1 order by id desc limit 1;";
        $select=$this->pdo->query($sql);
        $id=$select->fetchAll(PDO::FETCH_ASSOC);

        $filename = dirname(dirname(__FILE__))."\public\\".$id[0]['fiel_name'];

        $objPHPExcelReader = PHPExcel_IOFactory::load($filename); //加载excel文件
        
        $sheet = $objPHPExcelReader->getSheet(0); // 读取第一個工作表
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumm = $sheet->getHighestColumn(); // 取得总列数
        
        $highestColumm++;
        // $colIndex = A;
        $rowIndex = 2;
        for ($rowIndex; $rowIndex <= $highestRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始,列从A开始
            $a = 1;
            for ($colIndex = 'A'; $colIndex != $highestColumm; $colIndex++) {
                $addr = $colIndex . $rowIndex;
                $cell = $sheet->getCell($addr)->getValue();
                $data[$rowIndex][$a] = mb_convert_encoding($cell,"gbk","utf-8");
                $a++;
            }
            // $colIndex = A;
        }
        show($data);die;

        foreach($objPHPExcelReader->getWorksheetIterator() as $sheet){ //循环读取sheet
            foreach($sheet->getRowIterator() as $row){ //逐行处理
                
                if($row->getRowIndex()<2){ //确定从哪一行开始读取
                    continue;
                }
                $data[$row->getRowIndex()][] = '';
                foreach($row->getCellIterator() as $cell){ //逐列读取
                    // $a = $cell->getValue();
                    echo $cell."</br>";die;
                    
                    // $data[$row->getRowIndex()][]=$cell->getValue()?mb_convert_encoding($cell->getValue(),"gbk","utf-8"):'   ';
                }
                die;
            }
            die;
        }
        show($data);

 

posted @ 2018-08-21 10:42  远古狩猎采集者  阅读(4912)  评论(0编辑  收藏  举报