• PHPExcel读取excel文件


    PHPExcel是个很强大的类库,以前只使用过它生成Excel文件,非常方便。

    今天接到个项目要读取Excel的文件,以前也做过Excel转换写入数据库的工作, 

    不过相对简单一些,是转换成CSV格式再进行解析的。

    首先下载PHPExcel类库。http://phpexcel.codeplex.com/

    包含PHPExcel类库文件,如果不能确定文件类型的话可以使用PHPExcel_IOFactory::identify方法返回文件的类型,传递给该函数一个文件名就可以。

    然后根据返回的文件类型创建该类型的读取对象,进行文件的load。

    之后就可以进行数据的读取了,
    具体代码如下所示:

      1. <?php
      2.     require_once('include/common.inc.php');
      3.     require_once(ROOTPATH . 'include/phpExcel/PHPExcel/IOFactory.php');
      4.     
      5.     $filePath = './file/xls/110713.xls'; 
      6.     
      7.     $fileType = PHPExcel_IOFactory::identify($filePath); //文件名自动判断文件类型
      8.     $objReader = PHPExcel_IOFactory::createReader($fileType);
      9.     $objPHPExcel = $objReader->load($filePath);
      10.     
      11.     $currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
      12.     $allRow = $currentSheet->getHighestRow(); //行数
      13.     $output = array();
      14.     $preType = '';
      15.     
      16.     $qh = $currentSheet->getCell('A4')->getValue();
      17.     //按照文件格式从第7行开始循环读取数据
      18.     for($currentRow = 7;$currentRow<=$allRow;$currentRow++){ 
      19.         //判断每一行的B列是否为有效的序号,如果为空或者小于之前的序号则结束
      20.         $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
      21.         if(empty($xh))break;
      22.         
      23.         $tmpType = (string)$currentSheet->getCell('C'.$currentRow)->getValue(); //赛事类型
      24.         if(!empty($tmpType))$preType = $tmpType;
      25.         $output[$xh]['type'] = $preType;
      26.         $output[$xh]['master'] = $currentSheet->getCell('F'.$currentRow)->getValue(); //主队
      27.         $output[$xh]['guest'] = $currentSheet->getCell('H'.$currentRow)->getValue(); //客队    
      28.     }
      29.     
      30.     //从当前行开始往下循环,取出第一个不为空的行
      31.     for( ; ; $currentRow++){
      32.         $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
      33.         if(!empty($xh))break;
      34.     }
      35.     
      36.     for( ; $currentRow <= $allRow; $currentRow++){
      37.         $xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
      38.         if(empty($xh))break;
      39.         
      40.         $output[$xh]['rq'] = $currentSheet->getCell('I'.$currentRow)->getValue();
      41.     }
      42.     header("content-type:text/html; charset=utf-8");
      43.     
      44.     echo '期号:' . $qh . " ";
      45.     if(!empty($output)){
      46.         printf("%-5s %-15s %-40s %-40s %-5s ", '序号', '赛事类型', '主队', '客队', '让球值');
      47.         foreach($output as $key => $row){
      48.             $format = "%-5d %-15s %-40s %-40s %-5s ";
      49.             printf($format, $key, $row['type'], $row['master'], $row['guest'], $row['rq']);
      50.         }
      51.     }
      52. ?>
  • 相关阅读:
    关于Hyper-V备份的四大注意事项
    未找到导入的项目,请确认 <Import> 声明中的路径正确
    IDC门外汉-单线、双线、智能多线、BGP的区别
    国内主流云主机比较
    Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 2012 Backup
    [MSDN] Windows Server 2012 R2 简/繁/英下载
    深入浅出VC++串口编程之基于Win32 API
    Remon Spekreijse CSerialPort串口类的修正版2014-01-10
    Remon Spekreijse CSerialPort用法
    “CObject::operator =”: 无法访问 private 成员(在“CObject”类中声明)
  • 原文地址:https://www.cnblogs.com/wgx214/p/3709521.html
Copyright © 2020-2023  润新知