PHPExcel讀取excel檔案

來源:互聯網
上載者:User

標籤:class   code   tar   ext   int   http   

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 . "\n\n";
    45.     if(!empty($output)){
    46.         printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n", ‘序號‘, ‘賽事類型‘, ‘主隊‘, ‘客隊‘, ‘讓球值‘);
    47.         foreach($output as $key => $row){
    48.             $format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n";
    49.             printf($format, $key, $row[‘type‘], $row[‘master‘], $row[‘guest‘], $row[‘rq‘]);
    50.         }
    51.     }
    52. ?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.