zoukankan      html  css  js  c++  java
  • Thinkphp 使用PHPExcel导入,栗子

    栗子:

    <?php
        public function importData(){
            if ( !empty( $_FILES['file']['tmp_name'] ) ) {
                $uploadfile = $_FILES ['file'] ['tmp_name'];
        } else {
                $uploadfile = $_FILES['uploadfile']['tmp_name'];
            }
        
            if(empty($uploadfile) or !file_exists($uploadfile)){
                die('file not exists');
            }
            //Include path
            set_include_path ( get_include_path () . PATH_SEPARATOR . LIB_ROOT_PATH . '3rdParty/PHPExcel/' );
            // PHPExcel_IOFactory
            include 'PHPExcel.php';
            include 'PHPExcel/IOFactory.php';
        
            if (!class_exists('PHPExcel_IOFactory')) {
                //PHPExcel类未找到
                echo '未知错误!';
    
            }
        
                $inputFileType = PHPExcel_IOFactory::identify( $uploadfile );
        
                if ( in_array( $inputFileType, array ('Excel2007', 'Excel5')) ){
                    $objReader = PHPExcel_IOFactory::createReader( $inputFileType );
                    $objPHPExcel = $objReader->load( $uploadfile );
                    $sheetData = $objPHPExcel->getActiveSheet()->toArray( null, true, true, true );
                    $addData = array();
                    foreach ( $sheetData as $key => $value ) {
                        $addData['field1'] .= $value['A'].',';
                        $addData['field2'] .= $value['B'].',';
                        }
        
                    rtrim($addData['field1'],',');
        
                    $addData['field3'] = '测试参数';
                    $addData['type'] = 666;
        
                    $res = AppTools::webService('ModelModelnameModelname', 'getImport', array('params'=>$addData ));
        
                    if( $res['status'] == 0 && !empty($res['data']['fail']) ){
                        //存在导入失败的
                        $res['data']['fail'] = implode($res['data']['fail'],',');
                        $res['
    
     1. msg'] = sprintf($this->translator-
    
    >failedlist, mb_substr($res['data']['fail'],0,27,'utf-8'));
                        $res['status'] = 1;
                    }elseif( $res['status'] == 0 && empty($res['data']['fail']) ){
                        //全部导入成功
                        $res['msg'] = $this->translator->success;
                    }else{
                        //导入出错
                        $res['msg'] = $this->translator->failed;
                        $res['status'] = 'error';
                    }
        
                    session('arraylist',$res);
        
                    $this->redirect('index');
            }
        }
                
    ?>
  • 相关阅读:
    ROS学习(九)—— rqt_console 和 roslaunch
    ROS学习(八)—— 理解ROS服务和参数
    ROS学习(七)—— 理解ROS Topic
    ROS学习(六)—— 理解ROS节点
    ROS学习(五)—— 编译ROS Package
    ROS学习(四)—— 创建ROS Package
    jupyter notebook + pyspark 环境搭建
    快速搭建大数据分析虚拟机
    系统研究Airbnb开源项目airflow
    pip高级使用技巧以及搭建自己的pypi服务器
  • 原文地址:https://www.cnblogs.com/richardcastle/p/8297179.html
Copyright © 2011-2022 走看看