abdjll

abdjll

这家伙有点懒,还没写个性签名!

  • 财富值15
  • 威望值0
  • 总积分25

个人信息

  • 赞了评论

    不明觉厉0.0

  • 赞了评论

    补充1:

    在使用的过程中遇到了问题,①ord函数只能转义字符串首个字母的ASCII值,当excel表格的列超过Z列的时候(AA,AB,AC,...)的时候会出bug,
    这个时候只能获取到第一列数据的二维数组;②读取单元格内内容为数字为0的数据被array_filter过滤了;③增加第三个参数,读取某个sheet的数据, 不需要读取所以sheet的数据。读取excel数据的方法作出如下修改:

    
    public function getExcelData($filePath, $startRow = 1,$sheetIndex = 0) {
        $PHPExcel = new PHPExcel();
        /**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
        $PHPReader = new PHPExcel_Reader_Excel2007();
        //setReadDataOnly Set read data only 只读单元格的数据,不格式化 e.g. 读时间会变成一个数据等
        $PHPReader->setReadDataOnly(TRUE);
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new PHPExcel_Reader_Excel5();
            //setReadDataOnly Set read data only 只读单元格的数据,不格式化 e.g. 读时间会变成一个数据等
            $PHPReader->setReadDataOnly(TRUE);
            if (!$PHPReader->canRead($filePath)) {
                echo 'can not read excel';
                return;
            }
        }
    
        $PHPExcel = $PHPReader->load($filePath);
        //获取sheet的数量
        $sheetCount = $PHPExcel->getSheetCount();
        //获取sheet的名称
        $sheetNames = $PHPExcel->getSheetNames();
    
        //获取所有的sheet表格数据
        $emptyRowNum = 0;
        $i = $sheetIndex ;
        //超过范围
        if($i > ($sheetCount-1))
        {
            echo 'count error';
            return;
        }
    
        /**默认读取excel文件中的第一个工作表*/
        $currentSheet = $PHPExcel->getSheet($i);
        /**取得最大的列号*/
        $allColumn = $currentSheet->getHighestColumn();
        $allColumnIndex = PHPExcel_Cell::columnIndexFromString($allColumn);
        /**取得一共有多少行*/
        $allRow = $currentSheet->getHighestRow();
    
        $arr = array();
        for ($currentRow = $startRow; $currentRow <= $allRow; $currentRow++) {
            /**从第A列开始输出*/
            $ifhasZero = false;
            for ($currentColumn = 0; $currentColumn <= $allColumnIndex; $currentColumn++) {
                $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                if($val == '0') $ifhasZero = true;
                $arr[$currentRow][] = trim($val);
            }
    
            if ($ifhasZero == true) {
                foreach ($arr[$currentRow] as $key => $value) {
                    if($value === '') {
                        unset($arr[$currentRow][$key]);
                    }
                }
            }
    
            $arr[$currentRow] = $ifhasZero ? $arr[$currentRow] : array_filter($arr[$currentRow]);
            //统计连续空行
            if(empty($arr[$currentRow]) && $emptyRowNum <= 50) {
                $emptyRowNum++ ;
            } else {
                $emptyRowNum = 0;
            }
            //防止坑队友的同事在excel里面弄出很多的空行,陷入很漫长的循环中,设置如果连续超过50个空行就退出循环,返回结果
            //连续50行数据为空,不再读取后面行的数据,防止读满内存
            if($emptyRowNum > 50) {
                break;
            }
        }
    
        //只返回了第一个sheet的数据
        $returnData = $arr;
    
        //第一行数据就是空的,为了保留其原始数据,第一行数据就不做array_fiter操作;
        $returnData = $returnData && isset($returnData[$startRow]) && !empty($returnData[$startRow])  ? array_filter($returnData) : $returnData;
        return $returnData;
    }
    
    
    
  • 收藏了源码
    非常好用的PHPExcel导入导出类
  • 收藏了源码
    yii2.0 rbac权限 适合初学者易懂
  • 收藏了源码
    单文件上传(适合新手)
  • 赞了评论

    俺是 新手 希望各位 多多关照

试用期 等级规则
25/50
资料完整度
10/100
用户活跃度
0/100

Ta的关注

4

Ta的粉丝

0

Ta的访客

3