Sto usando PHPExcel per caricare file xls in un database mysql. Tutto funziona perfettamente, tranne una cosa: PHPExcel legge TUTTE le righe nel foglio di lavoro, anche quelle vuote e se, ad esempio, il mio xls ha 100 righe, il lettore scorre fino alla riga # 3359 (che suppongo sia il numero predefinito di righe per un foglio di lavoro vuoto).
Che cosa fa il lettore a contare anche le righe vuote? Questo è il mio codice:
$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();
}
Prima di leggere il file - filtri/cancelli le righe da esso? – matino
Νο, l'ho semplicemente caricato. – bikey77