PHPExcel 导入

首先:

//包含excel的类库
require APPPATH . 'third_party/PHPExcel.php';
require APPPATH . 'third_party/PHPExcel/Reader/Excel2007.php';

导入:

    $excel_reader = new PHPExcel_Reader_Excel2007();
        try {
            if (!$excel_reader->canRead($file_name)) {
                return $this->output("无法读取文件",1);
            }
            $obj = $excel_reader->load($file_name);
            // 读取excel文件中的第一个工作表
            $currentSheet = $obj->getSheet(0);
            //
            $allColumn = $currentSheet->getHighestColumn();
            //
            $allRow = $currentSheet->getHighestRow();
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($allColumn);//总列数
            $headtitle =array();
            for($cols =0 ;$cols<=$highestColumnIndex;$cols++){
                $headtitle[$cols] =(string)$currentSheet->getCellByColumnAndRow($cols, 1)->getValue();
            }
            if(empty($headtitle[0])){
                for($cols =0 ;$cols<=$highestColumnIndex;$cols++){
                    $headtitle[$cols] =(string)$currentSheet->getCellByColumnAndRow($cols, 2)->getValue();
                }
            }
           
            for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
                /**从第A列开始输出*/
                for($cols =0 ;$cols<=$highestColumnIndex;$cols++){
                    $data[$currentRow - 2][$cols] =(string)$currentSheet->getCellByColumnAndRow($cols, $currentRow)->getValue();
                }
            }
        } catch (Exception $e) {
            @unlink($file_name);
        
        }
        //删除临时文件
        @unlink($file_name);

再将导入数据保存到数据库即可。

转载于:https://www.cnblogs.com/DaBing0806/p/6599707.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值