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]; } } |
分类:
php
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】