Mervyn
Mervyn

Reputation: 891

PHPexcel cell caching

Am working with large excel file I have removed limits is there a way I can read through my cells much quicker. I have 6000 rows and reducing the tmp folder in my wamp folder.

set_time_limit(0);
ini_set('memory_limit', '-1');
include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings =    array( ' memoryCacheSize ' =>'8MB');    
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load('Book1.xlsx');
$highestRowEM = $objPHPExcel->getActiveSheet()->getHighestRow();
echo $highestRowEM;
for($i=0;$i<$highestRowEM;$i++){
    $varval=$objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
    if($varval==""){
        break;
    }
}
echo $i;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Book1.xlsx');

What to clean up workbooks with worksheets with , in them

  `include'../Classes/PHPExcel.php';
  include'../Classes/PHPExcel/IOFactory.php';
  set_time_limit(0);
  ini_set('memory_limit', '-1');
  $xlsxfiles=$_SESSION['file'];
  echo $xlsxfiles;
  echo "<br>";
  $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  $objPHPExcel = PHPExcel_IOFactory::load('../upload/'.$xlsxfiles);
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
  ////Validation
  foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
      $highestRow = $worksheet-> getHighestDataRow();
      $columnhighest=$worksheet->getHighestDataColumn();
      $columnhighestval=array_search($columnhighest, $alphabet);
      echo 'Worksheet - ' , $worksheet->getTitle()." Number of rows: ".$highestRow."<br>";
      for($cl=0;$cl<$highestRow+1;$cl++){
          for ($ga=1;$ga<$columnhighestval;$ga++){
              $letters=strtoupper($alphabet[$ga]);
              $clean=$worksheet->getCell($letters.$cl)->getValue();
              $cleandone=str_replace(','," ",$clean);
              $worksheet->setCellValue($letters.$cl,$cleandone);
          }
          $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      }
  }
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
  $objWriter->setOffice2003Compatibility(true);
  $objWriter->save('../upload/'.$xlsxfiles);
  echo "Done";

Upvotes: 1

Views: 11081

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

Tip 1:

replace

for($i=0;$i<$highestRowEM;$i++){
    $varval=$objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();

with

$objSheet = $objPHPExcel->getActiveSheet();
for($i=0;$i<$highestRowEM;$i++){
    $varval = $objSheet->getCell('A'.$i)->getValue();

Then you're not calling $objPHPExcel->getActiveSheet() in every iteration of the loop.

Then tell us what you're actually trying to do with the worksheet data, and we may be able to help speed it up a bit more

EDIT

Don't set the cell value unless you have to; Don't instantiate the Writer until you need it, and definitely not in the loop; Instead of using $letters and $alphabet, use the routines built-into PHPExcel, or take advantage of PHP's Perl-style character incrementor; Don't do maths in your loop comparisons

  ////Validation
  foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
      $highestRow = $worksheet->getHighestDataRow();
      $columnhighest=$worksheet->getHighestDataColumn();
      $columnhighest++;
      echo 'Worksheet - ' , $worksheet->getTitle()." Number of rows: ".$highestRow."<br>";
      for($cl = 0; $cl <= $highestRow; $cl++){
          for ($ga='A'; $ga !== $columnhighest; $ga++){
              $clean=$worksheet->getCell($ga.$cl)->getValue();
              $cleandone=str_replace(','," ",$clean);
              if($clean != $cleandone) {
                  $worksheet->setCellValue($ga.$cl, $cleandone);
              }
          }
      }
  }
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

Upvotes: 1

Related Questions