phpexcel 导入超过26列时的解决方案

浏览:7283 发布日期:2015/09/16 分类:心情闲聊 关键字: phpexcel 导入 导出
  //导入数据方法
    protected function goods_import($filename, $exts = 'xls') {
        //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
        import("Org.Util.PHPExcel");
        //创建PHPExcel对象,注意,不能少了\
        $PHPExcel = new \PHPExcel();
        //如果excel文件后缀名为.xls,导入这个类

        if ($exts == 'xls') {
            import("Org.Util.PHPExcel.Reader.Excel5");
            $PHPReader = new \PHPExcel_Reader_Excel5();
        } else if ($exts == 'xlsx') {
            import("Org.Util.PHPExcel.Reader.Excel2007");
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
        //载入文件
        $PHPExcel = $PHPReader->load($filename);
        //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
        $currentSheet = $PHPExcel->getSheet(0);
        //获取总列数
        $allColumn = $currentSheet->getHighestColumn();
        
        //获取总行数
        $allRow = $currentSheet->getHighestRow();
        ++$allColumn;
        
        //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
        for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
            //从哪列开始,A表示第一列
            for ($currentColumn = 'A'; $currentColumn !=$allColumn; $currentColumn++) {
                //数据坐标
                $address = $currentColumn . $currentRow;
                //读取到的数据,保存到数组$arr中
                $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
            }
        }
        $this->save_import($data);
    }
其实就修改 了两个地方,一个是加了一句++$allColumn;,第二是把原来的代码 for ($currentColumn = 'A'; $currentColumn <=$allColumn; $currentColumn++)修改为 for ($currentColumn = 'A'; $currentColumn !=$allColumn; $currentColumn++) 
最佳答案
评论( 相关
后面还有条评论,点击查看>>