在下面的脚本中,我收到的错误是前4行为空,尽管我使用的是包含数据的this spreadsheet。
请查看并提出问题所在:
$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...';
}
}
我正在使用PHPExcel 1.7.6版。
答案 0 :(得分: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
答案 1 :(得分:0)
我认为如果它不包含标题,你必须从零索引开始行。
$begin_row = 0; // 1st line of data in excel file
然后$ col&lt; = $ highestColumnIndex;
for ($row = $begin_row; $row <= $highestRow; $row++) {
$val=array();
for ($col=0; $col <= $highestColumnIndex; $col++) {
$cell = $objWorksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
答案 2 :(得分:0)
只是为了记录,如果有人遇到同样的问题:
当我意外地没有将Excel文件的路径传递给\PHPExcel_IOFactory::load()
,而是传递目录路径时,我完全观察到了所描述的行为。因此,如果您在调用getHighestRow()
时碰巧遇到不可用的值,请仔细检查路径是否实际正确。