php PHPExcel导入excel文件 简洁版

 

1、PHPExcel类库引入

       PHPExcel 在项目中并不能直接使用,需要先将它引入项目中,通过composer或者直接将PHPExcel类库的源码放到项目中。

 
安装命令:composer require phpoffice/phpexcel

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//如果使用thinkphp5.1框架,则直接use即可
use PHPExcel_IOFactory;
use PHPExcel;
 
/*
 * 导入
 */
function excelImport(){
 
    header("content-type:text/html;charset=utf8");
    date_default_timezone_set('PRC');
 
    //判断文件类型以及限制大小
    if (($_FILES["file"]["type"] == "application/vnd.ms-excel"|| $_FILES["file"]["type"]=="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && $_FILES["file"]["size"] < 2000000){
 
        if ($_FILES["file"]["error"] > 0) {
            $this->error( $_FILES["file"]["error"]);
        }
    }else{
        $this->error('文件类型错误或文件过大,上传失败');
    }
 
        $tmp_name = $_FILES["file"]["tmp_name"];
 
        //实例化类库 // 读取excel文件
       $inputFileType = PHPExcel_IOFactory::identify($tmp_name);
        $reader = \PHPExcel_IOFactory::createReader($inputFileType);
        $objData = $reader->load($tmp_name, $encode = 'utf-8');
     
        //读取数据
        $excel_array = $objData->getsheet(0)->toArray();
        //删除表格第一行
        unset($excel_array[0]);
    
    
//循环输出数据
foreach ($excel_array as $key => $value) {
          
              $data=[];
              $data['name']= $value[1];
              $data['title']= $value[2];
              $data['phone']= $value[3];
            
            
 
 
 
    }
 
}   

  



 
posted @   Mr、桔子  阅读(484)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示