当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP读取Excel类文件

PHP读取Excel类文件

2017年12月12日  | 移动技术网IT编程  | 我要评论

斑马骑士,花都区民政局,codeproject

想要使用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);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网