简体   繁体   中英

PHPExcel - read data of 2 columns

I want to get value of cells for several rows in a xls file with PHPExcel but it doesn't work. I want to read from line 22 at the end and get value of columns B and C of the row.

This is my code :

// Ouvre avec PHPExcel
$objPHPExcel = PHPExcel_IOFactory::load($fichier);

// Feuille active
$sheetObj = $objPHPExcel->getActiveSheet();

// Parcours des lignes à partir de la 22
foreach($sheetObj->getRowIterator(22,null) as $row){
    $num_colis = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow('B', $row)->getValue();
    $num_commande = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow('C', $row)->getValue(); 
}
$num_colis = $objPHPExcel->getActiveSheet()->rangeToArray('B1:C1')->getValue(); //array
$num_colis = $objPHPExcel->getActiveSheet()->getCell('B1')->getValue(); // string

Also you can do this to read cell in specific column dynamically:

$num = your num row
$num_colis = $objPHPExcel->getActiveSheet()->getCell('B'.$num)->getValue(); // string

Your loop must look like this:

$num = 22;
foreach($sheetObj->getRowIterator(22 ,null) as $row){
    $num_colis = $objPHPExcel->getActiveSheet()->getCell('B'.$num)->getValue();
    $num_commande = $objPHPExcel->getActiveSheet()->getCell('C'.$num)->getValue(); 
    $num++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM