找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 388|回复: 0

[composer组件] PHPexcel自动判断excel类型并读取excel所有sheet内容

  [复制链接]
发表于 2023-2-23 18:30 | 显示全部楼层 |阅读模式
     include "PHPExcel.php";
    /*
     * 读取excel表数据
     */
    public function readExcel($filename){
        $pathinfo = pathinfo(strtolower($filename));

        if($pathinfo['extension'] == 'xlsx'){
            $PHPReader = new PHPExcel_Reader_Excel2007();
            //if(!$PHPReader->canRead($filePath)){
        }elseif($pathinfo['extension'] == 'xls'){
            $PHPReader = new PHPExcel_Reader_Excel5();
        }else{
            return 'not support file type';
        }

        $PHPExcel   = $PHPReader->load($filename);
        // echo is_object($PHPExcel).'mmm';exit; //查看是否初始化成功

        //获取工作表的数目
        $sheetCount = $PHPExcel->getSheetCount();

        for($i = 0; $i < $sheetCount; $i++){

            /**读取excel文件中的第一个工作表*/
            $currentSheet = $PHPExcel->getSheet($i);

            /**取得最大的列号*/
            $allColumn = $currentSheet->getHighestColumn();

            /**取得一共有多少行*/
            $allRow = $currentSheet->getHighestRow();

            /**从第二行开始输出,因为excel表中第一行为列名*/
            for($currentRow = 1; $currentRow <= $allRow; $currentRow++){
                /**从第A列开始输出*/
                $row        = [];
                for($currentColumn= 'A'; $currentColumn <= $allColumn; $currentColumn++){
                    /**ord()将字符转为十进制数*/
                    $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();
                    /**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/

                    //$val = $currentSheet->getCell($currentColumn.$currentRow)->getValue();

                    if($val instanceof PHPExcel_RichText){//富文本转换字符串  
                        $val = $val->__toString();  
                    }
                    $row[] = $val;
                }
                //echo "</br>";
                if(!empty($row)) $sheetdata[]  = $row;
                unset($row);
            }

            $data['sheet'.$i] = $sheetdata;
            $currentSheet     = $sheetdata  = null;
        }
        return $data;
    }

打印结果$data:

Array
(
    [sheet0] => Array
        (
            [0] => Array
                (
                    [0] => id
                    [1] => uid
                    [2] => 昵称
                    [3] => 状态
                    [4] => 原因
                    [5] => ip
                    [6] => 手机
                    [7] => 时间
                )

            [1] => Array
                (
                    [0] => 592
                    [1] => 25
                    [2] => 0000null
                    [3] => 正常
                    [4] => 没有原因的原因
                    [5] => 195.168.1.5
                    [6] => 13800138000
                    [7] => 2016-11-23 10:11:49
                )
        )

    [sheet1] => Array
        (
            [0] => Array
                (
                    [0] => 列1
                    [1] => 列2
                    [2] => 列3
                )

            [1] => Array
                (
                    [0] => 111值1
                    [1] => 222佱2
                    [2] => 333值3
                )
        )

)

青云大叔出品,必属精品。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|学习笔记

GMT+8, 2024-9-8 10:32 , Processed in 0.020813 second(s), 14 queries , APCu On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表