zoukankan      html  css  js  c++  java
  • PHP导入Excel表格,读取Excel的内容到数组。

    PHP操作Excel,一般用到 PhpOffice/PhpExcel 传送门: https://github.com/PHPOffice/PHPExcel

    引用:

    require_once "/PhpOffice/PhpExcel/PHPExcel.php";

    引用以后就可以开始了

    public static function importExcel($file_path){
            $import_array = array();
            //获取文件读取操作
            $objReader = PhpOffice_PhpExcel_PHPExcel_IOFactory::load($file_path);
            //对象
            $sheets = $objReader->getAllSheets();
            $sheet = $sheets[0];
            $allRow = $sheet->getHighestRow(); // 取得总行数
            $allColumn = $sheet->getHighestColumn(); //取得总列数
    
            //每行的数据
            $val = array();
            //第一行代变列头,这里做key值
            $header = array();
    
            for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                $header_address = $currentColumn."1";
                $header_cell = $sheet->getCell($header_address);
                $header_cell = $header_cell->getValue();
                $header[$currentColumn] = Ap_Util_String::htmlspecialchars($header_cell); //处理了一下字符串
            }
    
            for($currentRow=2;$currentRow<=$allRow;$currentRow++){
                for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                    $address=$currentColumn.$currentRow;//数据坐标:A1,B1....
                    $cell =$sheet->getCell($address);
                    $cell = $cell->getValue();
                   
                    $val[$header[$currentColumn]] = Ap_Util_String::htmlspecialchars($cell); //处理了一下字符串
                        
    
                }
                $import_array[] = $val;
            }
            array_filter($import_array);
            return $import_array;
    }

    表格数据:

     

     导入的数据:

  • 相关阅读:
    根据当前日期转目的国地区时间戳
    时间戳转换作用域问题
    字符串拼接问题
    input全选和取消全选
    循环遍历渲染模块
    jQuery实现获取选中复选框的值
    React组件
    underscore.js依赖库函数分析二(查找)
    underscore.js依赖库函数分析一(遍历)
    React入门
  • 原文地址:https://www.cnblogs.com/Shadow3627/p/14185161.html
Copyright © 2011-2022 走看看