Home > php教程 > php手册 > phpexcel导入小例子

phpexcel导入小例子

WBOY
Release: 2016-06-07 11:43:57
Original
1585 people have browsed it

小实例,新人中的新人。请多多指教


<?php <br /> <br>           /* 补充说明下,很多人之所以出现各种问题,最大的原因就是导入问题,给出我 *的路径,ThinkPHP/Extend/Vendor.  放在这是因为最近用到比较多,到底放 *哪好,您自己看着办吧,如果只是偶尔用用,大可以直接扔在项目里。或者您 *干脆直接用我注释掉的路径里。我觉的这几行代码不需要什么多的注释, *我认为再多的注释都不如自己动手实验,大可以用断点法,查看所有步骤的内 *容。这比再多的注释都要来的实在 */ <br>           public function insertCus(){      <br>              if($_FILES['excel']['name']){    <br>                 /*有文件上传引用上传类,返回文件名*/             <br>                 $fileName=$this->_upload();               <br>                 if($fileName){                 <br>                 $file='./Public/Upload/Excel/'.date('Ymd').'/'.$fileName; <br>                  //导入excel类                 <br>                  vendor('phpExcel.PHPExcel');                 <br>                  vendor('phpExcel.PHPExcel.IOFactory');                 <br>                  vendor('phpExcel.PHPExcel.Reader.Excel5');                 <br>                  vendor('phpExcel.PHPExcel.Reader.Excel2007'); <br>  /* require_once './ThinkPHP/Extend/Library/ORG/Net/PHPExcel.class.php'; require_once './ThinkPHP/Extend/Library/ORG/Net/PHPExcel/IOFactory.php'; require_once './ThinkPHP/Extend/Library/ORG/Net/PHPExcel/Reader/Excel5.php';*/                       <br>                  /*获取Excel文件类型,确定版本*/                 <br>                  $extend=pathinfo($file);                 <br>                  $extend = strtolower($extend["extension"]);                 <br>                  $extend=='xlsx'?$reader_type='Excel2007':$reader_type='Excel5';                 <br>                  $objReader = PHPExcel_IOFactory::createReader($reader_type);                 <br>                  if(!$objReader){                     <br>                  $this->error('抱歉!excel文件不兼容。'); //执行失败,直接抛出错误中断                 <br>                  }                 <br>                  $objPHPExcel= $objReader->load($file);                  <br>                  $objWorksheet= $objPHPExcel->getActiveSheet();                 <br>                  $highestRow= $objWorksheet->getHighestRow();                   <br>                  $highestColumn = $objWorksheet->getHighestColumn();                  <br>                  $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数                 <br>                  $headtitle =array();                 <br>                  for($cols =0 ;$colsgetCellByColumnAndRow($cols, 1)->getValue();                  <br>                  }                 <br>                  if(empty($headtitle[0])){<br>                        for($cols =0 ;$cols                        $headtitle[$cols] =(string)$objWorksheet->getCellByColumnAndRow($cols, 2)->getValue();                     <br>                        }                 <br>                  }                                          <br>                  $strs=array();                             <br>                  /*第二行开始读取*/                 <br>                  for ($row =2;$row                       for($cols =0 ;$cols                      $strs[$row][$cols] =(string)$objWorksheet->getCellByColumnAndRow($cols, $row)->getValue();                     <br>                      }                 <br>                  }                 <br>                  dump($strs);//显示结果             <br>              }         <br>          }     <br>      }

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template