looping through sheets and selected columns

Topics: Developer Forum, User Forum
Jul 21, 2011 at 5:12 PM


i am having a issue with my code , i am using phpexcel to read a huge file . issue is my current file is of three sheet. if i upload complete file even specifying a  setActiveSheetIndex i get a error of memory that all 57 bit used but could not upload the file . so i have to upload file in parts

. i have to copy one sheet and paste it in a new file and then upload the data ! other thing is that file consist of 24 columns but

i only need to read specific columns only . i tired to implement the example in the phpexcel manual but i failed . i am attaching the code if

some could kindly provide solution for the following issues

1)loop through sheets without getting error of memory overflow

2)read only specific columns

Here is my code
set_time_limit (6000);  
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("2.xlsx");
$objWorksheet = $objPHPExcel->setActiveSheetIndex('0') ;
foreach ($objWorksheet->getRowIterator() as $row) 
$cellIterator = $row->getCellIterator();
if($dum) //to ignore first cell 
{                                                  .
foreach ($cellIterator as $cell) 
{    if($i==2||$i==3||$i==4||$i==9) //reading onlny columns B,C,D,I
          $num1=$value[$i]; // get the START VALUE
          $num2=$value[$i]; // get the STOP VALUE
          $asd=preg_split('#(?=\d)(?<=[a-z])#i',$value[$i] ); //convert strings as asd12321 to asd , 12321
          $value[$i]=$asd[1]; // to read only digit
          $value[$i]=$cell->getValue(); // read a string
          $value[$i+1]=$num2-$num1; //to take diff between the two 



$con =  mysql_connect("localhost","root","");
if (!$con)
 die('Could not connect: ' . mysql_error());
}mysql_select_db("mobilink", $con);
$sql="INSERT INTO my query ....";
if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());

} $dum=true; //to ignore the first row which was cloumns name


Jul 22, 2011 at 3:12 PM

will some one tell me how to loop through selected columns not all columns!! 

Aug 14, 2013 at 2:35 PM
http://stackoverflow.com/a/5578240/1707033 -> here you will find your answer