2011-08-24 126 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()