Home  >  Article  >  Backend Development  >  PHPExcel read excel

PHPExcel read excel

WBOY
WBOYOriginal
2016-07-29 09:15:031195browse

require_once './library/excel/PHPExcel.php';   
 
//要读的文件  
$filePath = 'test.xlsx';  
 
$PHPExcel = new PHPExcel();  
 
/**By default, excel2007 is used to read excel. If the format is incorrect, the previous version will be used to read it.*/   
$PHPReader = new PHPExcel_Reader_Excel2007();   
if(!$PHPReader->canRead($filePath))  
{  
    $PHPReader = new PHPExcel_Reader_Excel5();   
    if(!$PHPReader->canRead($filePath))  
    {   
        echo 'no Excel';   
        return ;   
    }  
}  
 
$PHPExcel = $PHPReader->load($filePath);   
/**Read the first worksheet in excel file*/   
$currentSheet = $PHPExcel->getSheet(0);   
/**Get the largest column number*/   
$allColumn = $currentSheet->getHighestColumn();  
/**Get the total number of rows*/   
$allRow = $currentSheet->getHighestRow();   
/**Start output from the second row because the first row in the excel table is the column name*/   
for($currentRow = 1;$currentRow <= $allRow;$currentRow++)  
{   
    /**Start output from column A*/   
    for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++)  
    {   
        $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord() converts characters into decimal numbers*/   
        echo $val."t";  
    }  
    echo "
";   
}  
 
?>

以上就介绍了PHPExcel 读excel,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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