zoukankan      html  css  js  c++  java
  • phpexcel 读取数据

    最近公司做一个客户导入会员的功能,以前导入都是使用csv格式导入的,但是客户反应问题挺多的,普遍是乱码(由于各种系统各种环境可能引起编码问题)。最近想着就把这个导入完全改成excel导入,就研究了下phpexcel,发现读取excel还是挺方便的,这样也方便客户了,比较excel 无论用office 还是 wps 打开都是没有问题的

    示例读取代码如下

    /*
     * $path excel文件路径
     * $header_mapping 文字和数据库字段的对应关系
     * excel第一行 是 字段标准(通常是汉字),
     * example
     * $header_mapping = [ '姓名' => 'uid' ];
     *
     */
    private function readExcel( $path,$header_mapping = []){
            $objPHPExcel = PHPExcel_IOFactory::load( $path );
            //选择标签页
    
            $sheet = $objPHPExcel->getSheet(0);
    
            //获取行数与列数,注意列数需要转换
            $highestRowNum = $sheet->getHighestRow();
            $highestColumn = $sheet->getHighestColumn();
            $highestColumnNum = PHPExcel_Cell::columnIndexFromString($highestColumn);
            $usefullColumnNum = $highestColumnNum;
    
            //取得字段,这里测试表格中的第一行为数据的字段,因此先取出用来作后面数组的键名
            $filed = array();
            for($i=0; $i<$highestColumnNum;$i++){
                $cellName = PHPExcel_Cell::stringFromColumnIndex($i).'1';
                $cellVal = $sheet->getCell($cellName)->getValue();//取得列内容
                if( !$cellVal ){
                    break;
                }
                $usefullColumnNum = $i;
                $filed []= $cellVal;
            }
    
            //开始取出数据并存入数组
            $data = [];
            for( $i=2; $i <= $highestRowNum ;$i++ ){//ignore row 1
                $row = array();
                for( $j = 0; $j <= $usefullColumnNum;$j++ ){
                    if( !isset($header_mapping[ $filed[$j] ]) ){
                        continue;
                    }
                    $cellName = PHPExcel_Cell::stringFromColumnIndex($j).$i;
                    $cellVal = $sheet->getCell($cellName)->getValue();
                    if($cellVal instanceof PHPExcel_RichText){ //富文本转换字符串
                        $cellVal = $cellVal->__toString();
                    }
    
                    $fd = $header_mapping[ $filed[$j] ];
                    $row[ $fd ] = $cellVal;
                }
                $data []= $row;
            }
            return $data;
    }
    
    /*
     * excel日期转化
     * excel中日期读取出来是个数字,需要转化
     **/
    private function excelTime( $date ){
        $date = date("Y-m-d",PHPExcel_Shared_Date::ExcelToPHP($date) );
        return  $date;
    }


    演示界面截图

    600


    600


    600


    phpexcel地址

    https://github.com/PHPOffice/PHPExcel 


    注意事项

    1:日期格式处理  ,处理如下

    //phpexcel 读取日期格式出来是个数字
    $date = date("Y-m-d",PHPExcel_Shared_Date::ExcelToPHP($date) );

    2:长整形处理

    //php读取excel长整形会以科学计数法记录
    number_format( $val,0,'','');



    原文地址:phpexcel 读取数据
    标签:phpexcel   read   读取   

    智能推荐

  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/apanly/p/5701961.html
Copyright © 2011-2022 走看看