亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1630 | 回復: 0
打印 上一主題 下一主題

PHPExcel讀取excel文件 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-12-22 08:54 |只看該作者 |倒序瀏覽
PHPExcel是個很強大的類庫,以前只使用過它生成Excel文件,非常方便。

今天接到個項目要讀取Excel的文件,以前也做過Excel轉換寫入數(shù)據(jù)庫的工作,

不過相對簡單一些,是轉換成CSV格式再進行解析的。

首先下載PHPExcel類庫。http://phpexcel.codeplex.com/

包含PHPExcel類庫文件,如果不能確定文件類型的話可以使用PHPExcel_IOFactory::identify方法返回文件的類型,傳遞給該函數(shù)一個文件名就可以。

然后根據(jù)返回的文件類型創(chuàng)建該類型的讀取對象,進行文件的load。

之后就可以進行數(shù)據(jù)的讀取了,
具體代碼如下所示:

  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(); //行數(shù)
  13.     $output = array();
  14.     $preType = '';
  15.     
  16.     $qh = $currentSheet->getCell('A4')->getValue();
  17.     //按照文件格式從第7行開始循環(huán)讀取數(shù)據(jù)
  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.     //從當前行開始往下循環(huán),取出第一個不為空的行
  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. ?>

您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP