php 上传文件读取多个工作表总结

这篇博客总结了如何使用PHP从Excel文件中读取多个工作表的数据。通过PHPExcel库,根据不同的文件扩展名(xlsx, xls, csv)加载文件,然后循环遍历每个工作表,获取最高列数和行数。博主详细展示了如何读取表头和具体数据,并将数据存储到二维数组中,最后进行数据验证和入库操作。" 128982652,16947570,电影推荐系统实践:ItemCF算法与GUI设计,"['推荐系统', 'Python', 'GUI开发', '数据挖掘']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

            //导入文件
            //从excel表格中读取信息
            
            $PHPExcel= new PHPExcel();
            $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
             //不同文件后缀的读取
            if ($extension =='xlsx') {
                $PHPReader = new PHPExcel_Reader_Excel2007();
                $PHPExcel = $PHPReader ->load($path.$name);
            } else if ($extension =='xls') {
                $PHPReader = new PHPExcel_Reader_Excel5();
                $PHPExcel = $PHPReader ->load($path.$name);
            } else if ($extension=='csv') {
                $PHPReader = new PHPExcel_Reader_CSV();
                //默认输入字符集
                $PHPReader->setInputEncoding('GBK');
                //默认的分隔符
                $PHPReader->setDelimiter(',');
                //载入文件
                $PHPExcel = $PHPReader->load($path.$name);
            }           

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

           /**循环读取多个工作表*/
            for ( $i = 0; $i < $sheetCount; $i++ ) {
                
                $currentSheet= $PHPExcel->getSheet($i);

                //print_r($sheetCount);exit;
                /**取得最大的列号*/
                $allColumn= $currentSheet->getHighestColumn();
                  
                /**取得一共有多少行*/
                $allRow= $currentSheet->getHighestRow();
               
                /**列名转化为数字*/
                $allColumnIndex = PHPExcel_Cell::columnIndexFromString($allColumn);

               

              $excelData = array();
                //取第三行的表头
                $col1 = $currentSheet->getCell('A3')->getValue();
                $col2 = $currentSheet->getCell('B3')->getValue();
                $col3 = $currentSheet->getCell('C3')->getValue();
                $col4 = $currentSheet->getCell('D3')->getValue();
                $col5 = $currentSheet->getCell('E3')->getValue();
                $col6 = $currentSheet->getCell('F3')->getValue();
                $col7 = $currentSheet->getCell('G3')->getValue();
                $col8 = $currentSheet->getCell('H3')->getValue();
                $col9 = $currentSheet->getCell('I3')->getValue();

              /*  从第四行开始取*/
                for($currentRow =4;(is_numeric($currentRow) && $currentRow <= $allRow) ;$currentRow++)               
                {
                    /*  
                     * 1、取值
                     * 2、验证格式。金额格式,身份证号不能重复,
                     * 3、入库                 
                    /* 从A列开始取 */
                    for($currentColumn =0;$currentColumn <= $allColumnIndex;$currentColumn++){
                       //$unitVal = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                       $excelData[$currentRow][] = (string)$currentSheet->getCellByColumnAndRow($currentColumn,        $currentRow)->getValue();
                      
                    }
                           
                                      
                   if(($excelData[$currentRow][2] != "") && is_numeric($excelData[$currentRow][0]))
                   {
                        //用户信息入库                   
                        $data_rs = User::add($excelData[$currentRow],$batchid,$form_rs,$currentRow,$i);

                     }


                   }

       }

 

              

 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值