phpexcel 讀取xls檔案
讀取xls檔案的方法很多,我們掌握其中一種即可。我用的是phpexcel類,有一點我也納悶,下載下來的檔案相當大,即使按網友們說的只要[img][/img],也是很大的,足有18M,下面我們看下phpexcel是如何讀取xls檔案的。
Php代碼 ?
- require?'excel/PHPExcel.php';?//負載檔案 ??
- ??$filename="D:/wamp/www/test/sysoa.xls"; ??
- ?//讀取2003以及之前版本的xls檔案 ??
- $phpreader=new?PHPExcel_Reader_Excel5(); ??
- if($phpreader->canRead($filename)){ ??
- ??
- ????$excel=$phpreader->load($filename); ??
- ????//取得當前worksheet ??
- ?? ??
- ????$cursheet=$excel->getSheet(0); ??
- ????//取得共有多少列,若不使用此靜態方法,獲得的$col是檔案列的最大的英文大寫字母 ??
- ????$col=PHPExcel_Cell::columnIndexFromString($cursheet->getHighestColumn()); ??
- ???? ??
- ????//取得共有多少行 ??
- ????$row=$cursheet->getHighestRow(); ??
- ??
- ????//迴圈擷取資料,xls檔案是列在前行在後比如第一行第二列,實際上xls是以B2來表達的 ??
- ??
- ????for($currow=1;$currow<=$row;$currow++){ ??
- ??????for($curcol=1;$curcol<=$col;$curcol++){ ??
- ???????????$result=$cursheet->getCellByColumnAndRow($curcol,$currow)->getValue(); ??
- ???????????if($result){ ??
- ???????????echo?'第'.$currow.'行第'.$curcol.'列:'.$result.' '; ??
- ???????????} ??
- ??????} ??
- ??????echo?'
'; ??
- ????} ??
- ????? ??
- }??
require 'excel/PHPExcel.php'; //負載檔案 $filename="D:/wamp/www/test/sysoa.xls"; //讀取2003以及之前版本的xls檔案 $phpreader=new PHPExcel_Reader_Excel5(); if($phpreader->canRead($filename)){ $excel=$phpreader->load($filename); //取得當前worksheet $cursheet=$excel->getSheet(0); //取得共有多少列,若不使用此靜態方法,獲得的$col是檔案列的最大的英文大寫字母 $col=PHPExcel_Cell::columnIndexFromString($cursheet->getHighestColumn()); //取得共有多少行 $row=$cursheet->getHighestRow(); //迴圈擷取資料,xls檔案是列在前行在後比如第一行第二列,實際上xls是以B2來表達的 for($currow=1;$currow<=$row;$currow++){ for($curcol=1;$curcol<=$col;$curcol++){ $result=$cursheet->getCellByColumnAndRow($curcol,$currow)->getValue(); if($result){ echo '第'.$currow.'行第'.$curcol.'列:'.$result.' '; } } echo '
'; } }
? 不過還有一些問題沒有解決,我在讀取另外一個xls檔案時, $row=$cursheet->getHighestRow();獲得的值竟然是65522,實在讓人納悶,但換一個又好了
1 樓 pz9042 2012-06-06
。。。這是我寫的。。