1

In this following script I'm getting an error that the first 4 rows are empty although I'm using this spreadsheet which contains data.

Please have a look and suggest what the problem might be:

$objPHPExcel = PHPExcel_IOFactory::load($path);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

    $highestRow = $objWorksheet->getHighestRow();
    $highestColumn = $objWorksheet->getHighestColumn();
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    $addedtags=0;
    $begin_row=2; // 1st line of data in excel file

    for ($row = $begin_row; $row <= $highestRow;  $row++) {
        $val=array();
        for ($col=0; $col < $highestColumnIndex; $col++) {
            $cell = $objWorksheet->getCellByColumnAndRow($col, $row);
            $val[] = $cell->getValue();
        }

        if ($val[0]<>'') { //check that row contains data before inserting
            $sql1 = sprintf("INSERT INTO ".$dbprefix."terms (name , slug, term_group) VALUES (%s, %s, %s)",
               GetSQLValueString($val[0], "text"),
               GetSQLValueString($val[1], "text"), 
               GetSQLValueString(0, "int"));
            $result = mysql_query($sql1) or die(mysql_error());
            echo '<br />Added: '.$val[0];
        } else {
            echo '<br />Error: Line '.$row.' was empty...';
        }
    } 

I'm using PHPExcel version 1.7.6.

4

3 回答 3

0

如果它不包含标题,我认为你必须从零索引开始行。

$begin_row = 0; // 1st line of data in excel file

然后 $col <= $highestColumnIndex;

for ($row = $begin_row; $row <= $highestRow;  $row++) {
    $val=array();
    for ($col=0; $col <= $highestColumnIndex; $col++) {
        $cell = $objWorksheet->getCellByColumnAndRow($col, $row);
        $val[] = $cell->getValue();
    }
于 2013-06-11T12:12:40.170 回答
0

尝试将$begin_row变量从 2 更改为 1 :

$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
$addedtags=0;
$begin_row=1; // 1st line of data in excel file

这是他们使用的示例$row=1

http://phpexcel.codeplex.com/discussions/393113

于 2013-06-10T21:18:57.553 回答
0

仅作记录,如果有人偶然发现同样的问题:

当我不小心没有将 Excel 文件的路径传递给 时,我准确地观察到了所描述的行为,而是传递了\PHPExcel_IOFactory::load()一个目录路径。因此,如果您在调用 时碰巧得到一个不可用的值getHighestRow(),请仔细检查路径是否真的正确。

于 2017-11-21T13:50:53.140 回答