当前位置:Gxlcms > PHP教程 > 读取excel文件,不得不读取A列

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

时间:2021-07-01 10:21:17 帮助过:38人阅读

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

function import_excel($filePath){
$txt=array();
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
echo 'no Excel';
return ;
}
}

//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath);

/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
echo "allcolumn:".$allColumn."
";
echo "allRow:".$allRow."
";
//循环读取每个单元格的内容。注意行从1开始,列从A开始
for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){
for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
echo $colIndex.",";
$addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
$txt[$rowIndex][]=$cell;
echo $cell,",";
}
echo "
";

}

return $txt;
}


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



引用:
for($colIndex='A';$colIndex<=$allColumn;$colIndex++){

$colIndex='A';
$colIndex++

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

改為
for($colIndex=0;$colIndex<=$allColumn;$colIndex++){
試試

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



Quote: 引用:

for($colIndex='A';$colIndex<=$allColumn;$colIndex++){

$colIndex='A';
$colIndex++

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

改為
for($colIndex=0;$colIndex<=$allColumn;$colIndex++){
試試


明白,如果這樣問題就不在這裏了。

人气教程排行