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;
    }

    表格数据:

     

     导入的数据:

  • 相关阅读:
    POJ--3164--Command Network【朱刘算法】最小树形图
    金典 SQL笔记(6)
    hdoj1106排序
    linux程序设计——运行SQL语句(第八章)
    iOS-UITextView-文本输入视图的使用
    HDU 5305 Friends(简单DFS)
    Android IntentService全然解析 当Service遇到Handler
    概要设计的要点
    DispatcherTimer
    原型模式
  • 原文地址:https://www.cnblogs.com/Shadow3627/p/14185161.html
Copyright © 2011-2022 走看看