PHPexcel单元缓存

时间:2012-12-11 14:44:17

标签: php caching phpexcel

使用大型excel文件我已经删除限制是否有一种方法可以更快地读取我的单元格。我有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');

如何使用工作表清理工作簿,其中包含

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

1 个答案:

答案 0 :(得分:1)

提示1:

替换

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

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

然后你不会在循环的每次迭代中调用$ objPHPExcel-&gt; getActiveSheet()。

然后告诉我们您实际尝试使用工作表数据做什么,我们可能会帮助加快它的速度

修改

除非必须,否则不要设置单元格值; 在您需要之前不要实例化Writer,并且绝对不在循环中; 而不是使用$ letters和$ alphabet,使用内置于PHPExcel中的例程,或利用PHP的Perl风格的字符增量器; 不要在循环比较中做数学

  ////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');