开发者

phpexcel selecting sheets and columns

开发者 https://www.devze.com 2023-03-22 06:00 出处:网络
I am currently working on a assignment where I am required to loop through a huge file of excel .I am using phpexcel for this job . I have two issues in this project

I am currently working on a assignment where I am required to loop through a huge file of excel .I am using phpexcel for this job . I have two issues in this project

  1. The issue is when I load the file I get memory full error .Even while using setActiveSheetIndex('') function , I am trying to read the code sheet by sheet, but at the moment I can not .

  2. I am required to pick up only selected columns not the whole row , I have tried to implement the example shown in phpexcel user guide but have failed to do so , I have to implement this to reduce the processing time .

Here is the code I am using

set_time_limit (6000);
require_once('classes/phpexcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("2.xlsx");
$objWorksheet = $objPHPExcel->setActiveSheetIndex('0') ;
$i=0;$dum=false;$sum=0;
foreach ($objWorksheet->getRowIterator() as $row) 
{
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); 
if($dum) //to ignore first cell 
{                                                  .
foreach ($cellIterator as $cell) 
{    if($i==2||$i==3||$i==4||$i==9)
    {
          if($i==2)
          {
          $value[$i]=$cell->getValue();
          $num1=$value[$i]; // get the starting date and time
          }
          if($i==3)
          {
          $value[$i]=$cell->getValue();
          $num2=$value[$i]; // get the ending date and time
          }
          if($i==4)
          { 
          $value[$i]=$cell->getValue(); 
          $asd=preg_split('#(?=\d)(?<=[a-z])#i',$value[$i] ); //convert strings as asd12321 to asd , 1232开发者_StackOverflow社区1
          $value[$i]=$asd[1]; // to read only digit
          }
          if($i==9)
          {
          $value[$i]=$cell->getValue(); // read a string
          $value[$i+1]=$num2-$num1; //to take diff in minutes between the two 
          }

    }$i++;

}$i=0;

$con =  mysql_connect("localhost","root","");
if (!$con)
{$value[0]=$value[2];
 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());
}
$sum++;


 mysql_close($con);
   }
 $dum=true;

}

I have to read only specific columns as I have total 26 columns in the file while I need only 4 . Help will be appreciated


In the documentation

http://phpexcel.codeplex.com/downloads/get/504328

In section 4.3 page 7 it describes how you can select specific columns instead of the whole spreadsheet.

class MyReadFilter implements PHPExcel_Reader_IReadFilter {

 public function readCell($column, $row, $worksheetName = '') { 
// Read rows 1 to 7 and columns A to E only 
if ($row >= 1 && $row <= 7) {
 if (in_array($column,range('A','E'))) {
 return true; 
   }
 } 
  return false;
 } 
} 
/** Create an Instance of our Read Filter **/

 $filterSubset = new MyReadFilter();
 [...]

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消