PHPExcel:读取单元格时出错

时间:2011-08-24 08:14:59

标签: phpexcel

我正在尝试使用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表是干净的。

有人遇到问题并解决了吗?

感谢。

2 个答案:

答案 0 :(得分:3)

你的循环尝试获取单元格11和12,...但据我记忆,getCell()方法需要像A1,A2这样的值!

所以你应该使用方法getCellByColumnAndRow($col,$row)

答案 1 :(得分:0)

您的代码已

$str.=$objPHPExcel->getActiveSheet()->getCell('$col.$row')->getValue()

你应该使用$ col和$ row的值的双引号。否则你使用'$ col。$ row'作为字符串

应该是

$str.=$objPHPExcel->getActiveSheet()->getCell("$col.$row")->getValue()
相关问题