2011-08-24 95 views
3

我试图用PHPExcel读取XLSX表格,并用其内容提供MySQL表格。PHPExcel:读取单元格时出错

我的问题是,在阅读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表格很干净。

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

谢谢。

回答

3

您的循环尝试获取单元格11和12,但是据我所知,getCell()方法需要像A1,A2这样的值!

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

0

您的代码

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

你应该有$关口和$行的值双引号中使用。否则,你用 '$关口。$行' 作为一个字符串

应该

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