zoukankan      html  css  js  c++  java
  • phpexcel读取excel的xls xlsx csv格式

    我之前写过一篇PHP读取csv文件的内容

    上代码index.php

    <?php
      
    
    /** 
     * 
     * @author XC
     * 
     */  
    class Excel
    {
        public $currentSheet;
        public $filePath;
        public $fileType;
        public $sheetIndex=0;
        public $allColumn;
        public $allRow;
        public function initialized($filePath) {
            if (file_exists($filePath)) {
                $this->filePath=$filePath;
            }else{
                return array();
            }
            //以硬盘方式缓存
            $cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_discISAM;
            $cacheSettings = array();
            PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
            $file_ext=strtolower(pathinfo($this->filePath, PATHINFO_EXTENSION));
            switch ($file_ext) {
                case 'csv':
                    $this->fileType='csv';
                    break;
                case 'xlsx':
                    $this->fileType='excel';
                    break;
                case 'xls':
                    $this->fileType='excel';
                    break;
                default:
                    $this->fileType='';
                    break;
            }
            
            if ($this->fileType=='csv') {
                $PHPReader = new PHPExcel_Reader_CSV();
      
                //默认字符集
                $PHPReader->setInputEncoding('GBK');
      
                //默认分隔符  
                $PHPReader->setDelimiter(',');
      
                if(!$PHPReader->canRead($this->filePath)){
                    return array();
                }
            }elseif ($this->fileType=='excel'){
                $PHPReader = new PHPExcel_Reader_Excel2007();
      
                if(!$PHPReader->canRead($this->filePath)){
                    $PHPReader = new PHPExcel_Reader_Excel5();
      
                    if(!$PHPReader->canRead($this->filePath)){
                        return array();
                    }
                }
            }else{
                return array();
            }
      
            $PHPReader->setReadDataOnly(true);
            $PHPExcel = $PHPReader->load($this->filePath);
            $this->currentSheet = $PHPExcel->getSheet((int)$this->sheetIndex);
            //$this->currentSheet = $PHPExcel->getActiveSheet();
            $this->allColumn=$this->currentSheet->getHighestColumn();
            $this->allRow=$this->currentSheet->getHighestRow();
        }
      
        public function fetch($beginRow=NULL, $endRow=NULL){
            $currentSheet=$this->currentSheet;
            $allColumn=$this->allColumn;$allRow=$this->allRow;
            $dataSrc=$data=array();
            
            //获取列标题
            for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
                $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, 1)->getValue();//ord()將字符转为十进制数  
                $dataSrc[ord($currentColumn) - 65]=strtolower(trim($val));}
                //echo implode("	", $dataSrc);
                $beginRow=$beginRow ? $beginRow : 2;
                $endRow=$endRow ? $endRow : $allRow;
                for($currentRow = $beginRow ;$currentRow <= $endRow ;$currentRow++){
                    //从第A列开始输出$dataRow=array();
                    for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
                        $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();//ord()將字符转为十进制数  
                        $dataRow[$dataSrc[ord($currentColumn) - 65]]=$val;
                        //单元级数据处理 ... 格式化日期等  
                    }
      
                    //行级数据处理 ...  
      
                    if($dataRow){
                        $data[]=$dataRow;}
                }
      
                //echo '<pre>', print_r($data), '</pre>';
      
                //echo "
    ";
            return $data;
        }
    }
    
    
    
    require_once 'Classes/PHPExcel.php';
    $import=new Excel(); 
    $import->initialized(dirname(__FILE__) . '/test.xlsx'); 
    header("Content-type: text/html; charset=utf-8"); 
    echo '<pre>', print_r($import->fetch()), '</pre>';
    
    
    
    ?>

    其中的$import->initialized(dirname(__FILE__) . '/test.xlsx');        test.xlsx修改为自己的execl路径

    require_once 'Classes/PHPExcel.php';修改为你自己PHPExcel.php所在的路径

    PHPExcel.php可从官网下载 http://phpexcel.codeplex.com/

  • 相关阅读:
    设计模式学习笔记-观察者模式
    谈C#中的Delegate
    EF 增删改查 泛型方法、类
    什么是表达式树,它与表达式、委托有什么区别?
    查询出各个学科的前3名的同学信息的Sql
    row_number() OVER(PARTITION BY)函数介绍
    Asp.net WebApi 项目示例(增删改查)
    ASP.NET WebAPI从入门
    .net中的Queue和Stack
    Replication--数据库镜像阻塞复制日志读取器的解决的办法
  • 原文地址:https://www.cnblogs.com/xcxc/p/4462654.html
Copyright © 2011-2022 走看看