1

我正在使用大型 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";
4

1 回答 1

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->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');
于 2012-12-11T14:56:58.290 回答