简体   繁体   English

PHPexcel单元缓存

[英]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. 在处理大型excel文件时,我已经消除了限制,因此我可以更快地读取单元格。 I have 6000 rows and reducing the tmp folder in my wamp folder. 我有6000行,并减少了wamp文件夹中的tmp文件夹。

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";

Tip 1: 秘诀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. 然后,您不必在循环的每次迭代中都调用$ objPHPExcel-> getActiveSheet()。

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; 在您需要它之前,不要实例化Writer,并且绝对不要循环使用它。 Instead of using $letters and $alphabet, use the routines built-into PHPExcel, or take advantage of PHP's Perl-style character incrementor; 不用使用$ letters和$ alphabet,而是使用内置于PHPExcel的例程,或者利用PHP的Perl样式的字符增量器。 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM