...

View Full Version : php Excel Reader code problem



ebookz
11-19-2012, 02:40 PM
This is my mydata.xls excel sheet image:http://i.imgur.com/LjPcw.png

php code :

<?php
// include class file
include("Excel/reader.php");

// initialize reader object
$excel = new Spreadsheet_Excel_Reader();

// read spreadsheet data...
$excel->read('mydata.xls');

// iterate over Excel sheet cells and save to database table//////////
$x=2;
while($x<=$excel->sheets[0]['numRows']) {

$y=1;
while($y<=$excel->sheets[0]['numCols']) {

//check empty cell...
if(isset($excel->sheets[0]['cells'][$x][$y])){
//save data to an array..
$z=$y-1;
$data[$z]=$excel->sheets[0]['cells'][$x][$y];

}
$y++;
} //end 2 while

//store array values to variables..
$st_id=$data[0];
$marks=$data[1];
$co_type=$data[2];
$status=$data[3];

echo $st_id."</br>";

$x++;
}//end 1 while

?>

out put :http://i.imgur.com/kTMBu.png
problem: how to stop showing last row repeated.....?????????????last row value print many no of times how to stop that?? plzz tell the code error & how to fix this problem???? Actually I'm trying to insert those data to my database table..before do that I tried to print one of this variable & see the result...

firepages
11-19-2012, 03:53 PM
even an empty excel file has a couple of thousand rows :(
so you need to query how many rows actually have data in them... or at least test for when the data stops.

I have never parsed excel spreadsheets but I guess you would watch for an empty cell to guess where the spreadsheet data is no more ... or set a flag.. this is a total guess by the way.



while($x<=$excel->sheets[0]['numRows']) {
if(empty($excel->sheets[0]['cells'][$x][0])){
#first cell is empty , is this the end of the usable data ?
break;
}
... etc


also please stop with the massive red text, its annoying ;)

ebookz
11-19-2012, 04:25 PM
even an empty excel file has a couple of thousand rows :(
so you need to query how many rows actually have data in them... or at least test for when the data stops.

I have never parsed excel spreadsheets but I guess you would watch for an empty cell to guess where the spreadsheet data is no more ... or set a flag.. this is a total guess by the way.



while($x<=$excel->sheets[0]['numRows']) {
if(empty($excel->sheets[0]['cells'][$x][0])){
#first cell is empty , is this the end of the usable data ?
break;
}
... etc


also please stop with the massive red text, its annoying ;)

I tried ur idea..but can not fix my error...still probelm is going

ebookz
11-19-2012, 04:57 PM
can anyone plzz tell me how to insert above excel sheet data to mysql daatabse by using phpexcel??????????

ebookz
11-19-2012, 05:26 PM
my actual problem is :Import Excel (XLS) document into MySQL database



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum