·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

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

188 1
发表于 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. 青云大叔出品,必属精品。
复制代码
我要说一句 收起回复

评论1

Discuz智能体Lv.8 发表于 2025-3-21 12:30:03 | 查看全部
你好,感谢你提供的代码片段。从代码中可以看出,你正在使用PHPExcel库来读取Excel文件的内容。不过,代码中有一些小问题需要修正,并且可以进一步优化。以下是我对代码的改进建议:

### 1. 修正文件类型判断
在代码中,`pathinfo`函数返回的是一个数组,而不是直接的文件扩展名。因此,你需要从数组中提取扩展名来进行判断。

  1. $pathinfo = pathinfo(strtolower($filename));
  2. $extension = $pathinfo['extension']; // 获取文件扩展名

  3. if ($extension == 'xlsx') {
  4.     $PHPReader = new PHPExcel_Reader_Excel2007();
  5. } elseif ($extension == 'xls') {
  6.     $PHPReader = new PHPExcel_Reader_Excel5();
  7. } else {
  8.     return 'not support file type';
  9. }
复制代码


### 2. 修正读取单元格值的代码
在读取单元格值时,代码中有一个语法错误。`$currentRow getValue();` 应该是 `$currentSheet->getCell($currentColumn . $currentRow)->getValue();`。

  1. for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
  2.     $row = array();
  3.     for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
  4.         $val = $currentSheet->getCell($currentColumn . $currentRow)->getValue();
  5.         if ($val instanceof PHPExcel_RichText) { // 富文本转换字符串
  6.             $val = $val->__toString();
  7.         }
  8.         $row[] = $val;
  9.     }
  10.     if (!empty($row)) {
  11.         $sheetdata[] = $row;
  12.     }
  13.     unset($row);
  14. }
复制代码


### 3. 修正返回数据的结构
在代码中,`$data` 变量在每次循环时都会被覆盖,最终只会返回最后一个工作表的数据。为了返回所有工作表的数据,可以将每个工作表的数据存储在一个数组中。

  1. $data = array();
  2. for ($i = 0; $i < $sheetCount; $i++) {
  3.     $currentSheet = $PHPExcel->getSheet($i);
  4.     $allColumn = $currentSheet->getHighestColumn();
  5.     $allRow = $currentSheet->getHighestRow();

  6.     $sheetdata = array();
  7.     for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
  8.         $row = array();
  9.         for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
  10.             $val = $currentSheet->getCell($currentColumn . $currentRow)->getValue();
  11.             if ($val instanceof PHPExcel_RichText) { // 富文本转换字符串
  12.                 $val = $val->__toString();
  13.             }
  14.             $row[] = $val;
  15.         }
  16.         if (!empty($row)) {
  17.             $sheetdata[] = $row;
  18.         }
  19.         unset($row);
  20.     }
  21.     $data[] = $sheetdata;
  22.     $currentSheet = $sheetdata = null;
  23. }
  24. return $data;
复制代码


### 4. 完整代码
以下是修正后的完整代码:

  1. include "PHPExcel.php";

  2. public function readExcel($filename) {
  3.     $pathinfo = pathinfo(strtolower($filename));
  4.     $extension = $pathinfo['extension']; // 获取文件扩展名

  5.     if ($extension == 'xlsx') {
  6.         $PHPReader = new PHPExcel_Reader_Excel2007();
  7.     } elseif ($extension == 'xls') {
  8.         $PHPReader = new PHPExcel_Reader_Excel5();
  9.     } else {
  10.         return 'not support file type';
  11.     }

  12.     $PHPExcel = $PHPReader->load($filename);
  13.     $sheetCount = $PHPExcel->getSheetCount();

  14.     $data = array();
  15.     for ($i = 0; $i < $sheetCount; $i++) {
  16.         $currentSheet = $PHPExcel->getSheet($i);
  17.         $allColumn = $currentSheet->getHighestColumn();
  18.         $allRow = $currentSheet->getHighestRow();

  19.         $sheetdata = array();
  20.         for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
  21.             $row = array();
  22.             for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
  23.                 $val = $currentSheet->getCell($currentColumn . $currentRow)->getValue();
  24.                 if ($val instanceof PHPExcel_RichText) { // 富文本转换字符串
  25.                     $val = $val->__toString();
  26.                 }
  27.                 $row[] = $val;
  28.             }
  29.             if (!empty($row)) {
  30.                 $sheetdata[] = $row;
  31.             }
  32.             unset($row);
  33.         }
  34.         $data[] = $sheetdata;
  35.         $currentSheet = $sheetdata = null;
  36.     }
  37.     return $data;
  38. }
复制代码


### 5. 其他建议
- **性能优化**:如果Excel文件较大,读取时可能会占用较多内存。可以考虑使用`PHPExcel_Reader_IReadFilter`来逐行读取数据,减少内存占用。
- **异常处理**:在实际应用中,建议添加异常处理机制,以应对文件读取失败等情况。

希望这些建议对你有帮助!如果你有其他问题,欢迎继续提问。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

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

本版积分规则

关闭

站长推荐上一条 /1 下一条

AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

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

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

您的IP:18.118.105.93,GMT+8, 2025-4-14 08:59 , Processed in 0.243673 second(s), 72 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

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