·设为首页收藏本站📧邮箱修改🎁免费下载专区🔐设置/修改密码👽群雄群聊
返回列表 发布新帖

PHPexcel自动判断excel类型并读取excel所有sheet内容

137 0
发表于 2023-4-21 23:34:58 | 显示全部楼层 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1.      include "PHPExcel.php";
  2.     /*
  3.      * 读取excel表数据
  4.      */
  5.     public function readExcel($filename){
  6.         $pathinfo = pathinfo(strtolower($filename));
  7.         if($pathinfo['extension'] == 'xlsx'){
  8.             $PHPReader = new PHPExcel_Reader_Excel2007();
  9.             //if(!$PHPReader->canRead($filePath)){
  10.         }elseif($pathinfo['extension'] == 'xls'){
  11.             $PHPReader = new PHPExcel_Reader_Excel5();
  12.         }else{
  13.             return 'not support file type';
  14.         }
  15.         $PHPExcel   = $PHPReader->load($filename);
  16.         // echo is_object($PHPExcel).'mmm';exit; //查看是否初始化成功
  17.         //获取工作表的数目
  18.         $sheetCount = $PHPExcel->getSheetCount();
  19.         for($i = 0; $i < $sheetCount; $i++){
  20.             /**读取excel文件中的第一个工作表*/
  21.             $currentSheet = $PHPExcel->getSheet($i);
  22.             /**取得最大的列号*/
  23.             $allColumn = $currentSheet->getHighestColumn();
  24.             /**取得一共有多少行*/
  25.             $allRow = $currentSheet->getHighestRow();
  26.             /**从第二行开始输出,因为excel表中第一行为列名*/
  27.             for($currentRow = 1; $currentRow <= $allRow; $currentRow++){
  28.                 /**从第A列开始输出*/
  29.                 $row        = [];
  30.                 for($currentColumn= 'A'; $currentColumn <= $allColumn; $currentColumn++){
  31.                     /**ord()将字符转为十进制数*/
  32.                     $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();
  33.                     /**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
  34.                     //$val = $currentSheet->getCell($currentColumn.$currentRow)->getValue();
  35.                     if($val instanceof PHPExcel_RichText){//富文本转换字符串  
  36.                         $val = $val->__toString();  
  37.                     }
  38.                     $row[] = $val;
  39.                 }
  40.                 //echo "</br>";
  41.                 if(!empty($row)) $sheetdata[]  = $row;
  42.                 unset($row);
  43.             }
  44.             $data['sheet'.$i] = $sheetdata;
  45.             $currentSheet     = $sheetdata  = null;
  46.         }
  47.         return $data;
  48.     }
  49. 打印结果$data:
  50. Array
  51. (
  52.     [sheet0] => Array
  53.         (
  54.             [0] => Array
  55.                 (
  56.                     [0] => id
  57.                     [1] => uid
  58.                     [2] => 昵称
  59.                     [3] => 状态
  60.                     [4] => 原因
  61.                     [5] => ip
  62.                     [6] => 手机
  63.                     [7] => 时间
  64.                 )
  65.             [1] => Array
  66.                 (
  67.                     [0] => 592
  68.                     [1] => 25
  69.                     [2] => 0000null
  70.                     [3] => 正常
  71.                     [4] => 没有原因的原因
  72.                     [5] => 195.168.1.5
  73.                     [6] => 13800138000
  74.                     [7] => 2016-11-23 10:11:49
  75.                 )
  76.         )
  77.     [sheet1] => Array
  78.         (
  79.             [0] => Array
  80.                 (
  81.                     [0] => 列1
  82.                     [1] => 列2
  83.                     [2] => 列3
  84.                 )
  85.             [1] => Array
  86.                 (
  87.                     [0] => 111值1
  88.                     [1] => 222佱2
  89.                     [2] => 333值3
  90.                 )
  91.         )
  92. )
  93. 青云大叔出品,必属精品。
复制代码
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图

您的IP:18.225.57.49,182.106.158.74,GMT+8, 2024-4-29 11:08 , Processed in 0.213766 second(s), 68 queries , Gzip On, Redis On.

Based on Discuz! W1.0 Licensed

© 2001-2024 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表