【问题标题】:PHPExcel : Error while reading cellsPHPExcel:读取单元格时出错
【发布时间】:2011-11-02 14:21:18
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: phpexcel


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你的代码有

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

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

      应该是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 1970-01-01
        • 2017-04-19
        • 1970-01-01
        相关资源
        最近更新 更多