PHPExcel : Error while reading cells
I'm trying to read a XLSX sheet with PHPExcel, and feed a MySQL table with its 开发者_如何学运维content.
My problem is that while reading the XLSX, I get the following error message :
Fatal error: Uncaught exception 'Exception' with message 'Cell coordinate must not be absolute.' in C:\Program Files\EasyPHP-5.3.3\www\alliance_run\phpexcel\Classes\PHPExcel\Worksheet.php:954 Stack trace: #0 C:\Program Files\EasyPHP-5.3.3\www\alliance_run\__essai_DB.php(22): PHPExcel_Worksheet->getCell('$col.$row') #1 {main} thrown in C:\Program Files\EasyPHP-5.3.3\www\alliance_run\phpexcel\Classes\PHPExcel\Worksheet.php on line 954
Here is my code :
mysql_connect("localhost", "root", "");
mysql_select_db("alliance_run");
// vidage de la table test_equipement
$query1 ="TRUNCATE TABLE `test_equipement` ";
$resultat = mysql_query($query1);
require_once 'phpexcel/Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("edf/equipement.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 1; $col <= $highestColumnIndex; ++$col) {
$str.=$objPHPExcel->getActiveSheet()->getCell('$col.$row')->getValue().'\\';
}
$strs=explode("\\",$str);
$query ="INSERT INTO test_equipement (numero_serie, code_site, code_produit, tag, date_installation, date_acceptation, code_fournisseur, client) VALUES ('".
$strs[0]."','". // numero_serie
$strs[1]."','".// code_site
$strs[2]."','".// code_produit
$strs[3]."','".// tag
$strs[4]."','".// date_installation
$strs[5]."','".// date_acceptation
$strs[6]."','".// code_fournisseur
$strs[7]."')";// client
mysql_query($query);
}
The problem seems to be on this line :
$str.=$objPHPExcel->getActiveSheet()->getCell('$col.$row')->getValue().'\\';
I've tried the different following codes, without success :
$str.=$objPHPExcel->getActiveSheet()->getCell($col$row)->getValue().'\\';
$str.=$objPHPExcel->getActiveSheet()->getCell('$col.$row')->getValue().'\\';
$str.=$objPHPExcel->getActiveSheet()->getCell("$col.$row")->getValue().'\\';
$str.=$objPHPExcel->getActiveSheet()->getCell($col.$row)->getValue().'\\';
$str.=$objPHPExcel->getActiveSheet()->getCell('$col$row')->getValue().'\\';
$str.=$objPHPExcel->getActiveSheet()->getCell("$col$row")->getValue().'\\';`
I precise that my XSLX sheet is clean.
Did someone encounter the problem, and solved it ?
Thanks.
Your loop tries to get the cell 11, and 12, ... But as far as I remember, the getCell()
method needs values like A1,A2 !
So you should use the method getCellByColumnAndRow($col,$row)
.
your code has
$str.=$objPHPExcel->getActiveSheet()->getCell('$col.$row')->getValue()
you should have double quotes for the values of $col and $row to be used. otherwise you use '$col.$row' as a string
it should be
$str.=$objPHPExcel->getActiveSheet()->getCell("$col.$row")->getValue()
精彩评论