Home  >  Article  >  Backend Development  >  读取excel文件,不得不读取A列

读取excel文件,不得不读取A列

WBOY
WBOYOriginal
2016-06-13 12:18:07854browse

读取excel文件,只能读取A列

<br />function import_excel($filePath){<br />  $txt=array();   <br />   $PHPReader = new PHPExcel_Reader_Excel2007();  <br />if(!$PHPReader->canRead($filePath)){  <br />    $PHPReader = new PHPExcel_Reader_Excel5();  <br />    if(!$PHPReader->canRead($filePath)){  <br />        echo 'no Excel';  <br />        return ;  <br />    }  <br />}   <br /><br />//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件  <br />$PHPExcel = $PHPReader->load($filePath);  <br />  <br />/**读取excel文件中的第一个工作表*/  <br />$currentSheet = $PHPExcel->getSheet(0);  <br />/**取得最大的列号*/  <br />$allColumn = $currentSheet->getHighestColumn();  <br />/**取得一共有多少行*/  <br />$allRow = $currentSheet->getHighestRow();  <br />  echo "allcolumn:".$allColumn."<br>";<br />  echo "allRow:".$allRow."<br>";<br />//循环读取每个单元格的内容。注意行从1开始,列从A开始  <br />for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){  <br />    for($colIndex='A';$colIndex<=$allColumn;$colIndex++){  <br />        echo $colIndex.",";<br />        $addr = $colIndex.$rowIndex;  <br />        $cell = $currentSheet->getCell($addr)->getValue();  <br />        $txt[$rowIndex][]=$cell;<br />        echo $cell,",";<br />    } <br />    echo "<br>"; <br />  <br />}      <br />    <br />   return $txt; <br />}<br />


只能读取A列的内容,后面的都读不到,网上的示例都是这样的
是不是 for($colIndex='A';$colIndex------解决思路----------------------
请不要误导,楼主的代码并无大问题
for($i='A'; $i!='AA'; $i++) echo $i;
ABCDEFGHIJKLMNOPQRSTUVWXYZ



引用:
for($colIndex='A';$colIndex
$colIndex='A';
$colIndex++

A是字符,執行++ 只會顯示是1
所以每次都是1

改為
for($colIndex=0;$colIndex試試

------解决思路----------------------
引用:
请不要误导,楼主的代码并无大问题
for($i='A'; $i!='AA'; $i++) echo $i;
ABCDEFGHIJKLMNOPQRSTUVWXYZ



Quote: 引用:

for($colIndex='A';$colIndex
$colIndex='A';
$colIndex++

A是字符,執行++ 只會顯示是1
所以每次都是1

改為
for($colIndex=0;$colIndex試試


明白,如果這樣問題就不在這裏了。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn