PHPExcel reading past $ highRow

I am using PHPExcel to upload xls files to mysql database. Everything works fine, except for one: PHPExcel reads ALL lines on the sheet, even empty ones, and if, for example, my xls has 100 lines, the reader completely wraps to line # 3359 (which, I assume, the default number of lines for an empty sheet )

What makes the reader even read empty lines? This is my code:

$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(); } 
+6
source share
2 answers

3359 is not the default number of rows for an empty sheet ... this means that the one who created the worksheet actually created it with these empty lines (probably unintentionally). The reader doesn’t care if the line is empty or not, you told her to read the worksheet, and he does just that.

If you do not know exactly how many lines you want to read before loading the book (in this case, you can apply a read filter to read only these lines), you need to check each line to see not inside the for loop.

+1
source

I used this to filter out empty lines. Although I print them on my screen for editing, it may be useful for you:

 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){} } } 
+3
source

Source: https://habr.com/ru/post/903894/


All Articles