gpt4 book ai didi

PHPExcel 读取过去的 $highestRow

转载 作者:可可西里 更新时间:2023-11-01 06:48:09 28 4
gpt4 key购买 nike

我正在使用 PHPExcel 将 xls 文件加载到 mysql 数据库中。除了一件事,一切都工作正常:PHPExcel 读取工作表中的所有行,甚至是空白行,例如,如果我的 xls 有 100 行,读取器一直循环到行 #3359(我想是空白工作表的默认行数)。

是什么让读者连空白行都数?这是我的代码:

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

$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;

$rowsadded=0;
$begin_row=5; // 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]<>'' && $val[1]<>'') { //check that row contains data before inserting

$rowsadded++;
$sql1 = sprintf("INSERT INTO aitisi (name_u, onoma_u, asf_gr, code, compid, im_egr, im_exp, symb, programa, cost, showcost, omadiko, ar_tayt, afm, dieyth, poli, til_st, til_kin, ep_te1, on_te1, ep_te2, on_te2, ep_te3, on_te3, ep_te4, on_te4, name_us, onoma_us) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($val[0], "text"),
GetSQLValueString($val[1], "text"),
...
GetSQLValueString($val[11], "text"),
GetSQLValueString($val[12], "text"),
GetSQLValueString($val[13], "text"),
GetSQLValueString($val[14], "text"),
GetSQLValueString($val[15], "text"),
GetSQLValueString($val[16], "text"),
GetSQLValueString($val[17], "text")).';';
$result = mysql_query($sql1) or die(mysql_error());
$field_id = mysql_insert_id();
}

最佳答案

我用它来过滤空行。虽然我将它们打印在我的屏幕上进行编辑,但也许它会对你有用:

foreach($worksheet->getRowIterator() as $row) {
$range = 'A'.$row->getRowIndex().':'.$highestColumn.$row->getRowIndex();
$rowData = $worksheet->rangeToArray( $range, NULL,TRUE,TRUE,TRUE);
$rowData = $rowData[$row->getRowIndex()];
if(implode("",$rowData) != "") {
foreach($rowData as $column => $value){}
}
}

关于PHPExcel 读取过去的 $highestRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8531171/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com