PHP读取Excel类文件

yipeiwu_com6年前PHP代码库

想要使用PHP读取Excel文件必然要用到PHPExcel开源类库,网上资源应该挺多的。但是每一种的操作必然都是不同的,可原理应该都是大同小异。

这个文件夹里包含的就是PHPExcel类文件

,在外面还有一个入口PHP文件

处理机制: 1.读取Excel文件

           2.获取最大行号和最大列号

              3.通过行数循环里面嵌套列数循环来用特殊符号拼接每个小表格里面的数据得到一个字符串

           4.然后使用explode拆分函数将字符串拆分后就得到了一个二维数组(即表格里面的数据)。

代码示例

$path = "/wamp/www/xiong/hadf";
    if(is_dir($path)){
      echo "存在";
    }else{
      mkdir($path, 0777, true);
      echo "chuangji";
    }
    require_once"./PHPExcel.php";
    $filePath = "./1233.xlsx";
    $PHPReader = new PHPExcel_Reader_Excel2007();
          if(!$PHPReader->canRead($filePath)) {
            $PHPReader = new PHPExcel_Reader_Excel5();
            if(!$PHPReader->canRead($filePath)) {
              echo 'no Excel';
              exit;
            }
          }
    $PHPExcel = $PHPReader->load($filePath);
          /**读取excel文件中的第一个工作表*/
          $currentSheet = $PHPExcel->getSheet(0);
          /**取得最大的列号*/
          $allColumn = $currentSheet->getHighestColumn();
          // echo $allColumn;exit;
          /**取得一共有多少行*/
          $allRow = $currentSheet->getHighestRow();
          /**从第二行开始输出,因为excel表中第一行为列名*/
          $val = '';
          for($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
          /**从第A列开始输出*/
            for($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
              $val .= $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow);//->getValue();/**ord()将字符转为十进制数*/
              $val .= "&%|%&";
              /**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将GBK编码转为UTF-8编码输出*/
              //$val .= iconv('GBK','UTF-8', $val);
            }
            $val .= "\n";
          }$member_info_arr = explode("\n", $val);
          unset($member_info_arr[count($member_info_arr) - 1]);
          $present_time = date("Y-m-d H:i:s");
          if(count($member_info_arr) <= 0) {
            sys_msg_json(0, "会员信息文件中无数据,请添加");
          }
          unset($member_info_arr[0]);
          // var_dump($member_info_arr);exit;
          foreach ($member_info_arr as $key => $val) {
            $arr[$key] = explode("&%|%&",$val);
          }
          // var_dump($arr);exit;
          foreach ($arr as $key => $val) {
            unset($arr[$key][2]);
          }
          var_dump($arr);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持【宜配屋www.yipeiwu.com】!

相关文章

php下网站防IP攻击代码,超级实用

今天我开发了下面的代码,算是大功初成,一天拦截了15个IP,服务器负载正常。 复制代码 代码如下: <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADD...

PHP生成随机码的思路与方法实例探索

本文实例讲述了PHP生成随机码的思路与方法。分享给大家供大家参考,具体如下: 背景 今天因为无聊,小伙伴让写一个生成5位随机码的函数,要求:可包含数字、字母大小写,代码尽量短。 解题思路...

php 中self,this的区别和操作方法实例分析

本文实例讲述了php 中self,this的区别和操作方法。分享给大家供大家参考,具体如下: 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人...

使用PHP数组实现无限分类,不使用数据库,不使用递归.

复制代码 代码如下:<?php class cat {     public $data;   &n...

php常用字符串查找函数strstr()与strpos()实例分析

本文实例讲述了php常用字符串查找函数strstr()与strpos()。分享给大家供大家参考,具体如下: 一句话使用strpos判断 ===或!==,这样才能达到预期的效果,性能要比s...