zoukankan      html  css  js  c++  java
  • 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);

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

  • 相关阅读:
    常用颜色
    在VS2010中打开VS2012的项目
    vs2012 断点不能调试
    Setup Factory 打包.netframework 2.0
    Access 中数据库操作时提示from子句语法错误
    vs2012 .netFramwork2.0发布到xp
    c# access插入null值
    Visual Studio安装卸载模板
    Codeforces 455D
    ACdream 1157 (cdq分治)
  • 原文地址:https://www.cnblogs.com/DaBing0806/p/6599707.html
Copyright © 2011-2022 走看看