我正在尝试使用 PHPExcel 读取 XLSX 工作表,并将其内容提供给 MySQL 表。
我的问题是,在阅读 XLSX 时,我收到以下错误消息:
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
这是我的代码:
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);
}
问题似乎出在这一行:
$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().'\\';
$str.=$objPHPExcel->getActiveSheet()->getCell("$col$row")->getValue().'\\';`
我准确地说我的 XSLX 工作表是干净的。
有人遇到过这个问题并解决了吗?
谢谢。
最佳答案
你的循环试图获取单元格 11 和 12,...但据我所知,getCell()
方法需要像 A1,A2 这样的值!
所以你应该使用方法getCellByColumnAndRow($col,$row)
。
关于PHPExcel : Error while reading cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7172406/