使用SqlLite缓存方法在phpExcel中耗尽内存

时间:2016-01-03 15:38:17

标签: php excel caching

我正在构建一个从数据库中获取数据的excel文件。在某些情况下,excel在行和列方面都可以是非常大的。在这种情况下,我得到关于内存耗尽的错误。

阅读其他SO问题中给出的建议我设置了我的代码,使用此代码使用SqlLite3缓存方法:

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$data_rif.'_'.$nome.'_business_plan.xlsx"');
//header('Content-Disposition: attachment;filename="'.$data_rif.'_business_plan.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
if (PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
    echo date('H:i:s') , " Enable Cell Caching using " , $cacheMethod , " method" , EOL;
} else {
    echo date('H:i:s') , " Unable to set Cell Caching using " , $cacheMethod , " method, reverting to memory" , EOL;
}
// Create a new PHPExcel object 
$ea = new PHPExcel();
$ea->getProperties()
   ->setCreator($nome)
   ->setTitle('Business Plan')
   ->setLastModifiedBy($nome)
   ->setDescription('Jenius Business Plan')
   ->setSubject('Jenius Business Plan')
   ->setKeywords('excel jenius businessplan')
   ->setCategory('financial')
   ; 
$ews = $ea->getSheet(0);

在这次修改之后,精化时间变得越来越大但我仍然遇到了耗尽的内存错误:

  


致命错误:允许   内存大小耗尽314572800字节(试图分配85个字节)   在    /var/www/jdev/creditmanager/Classes/PHPExcel/CachedObjectStorage/CacheBase.php 的   在线 186

缓存设置有什么问题?

创建了这个代码的更小的excel而没有任何不便

这是我如何填充excel的子集

foreach($creds as $cred){
    // dati generali dei singoli crediti
    $nome_soff = soff2name($cred['cod_soff'],$pdo);
    $data_acq = mysql2table($cred['data_acq']);
    $ammesso = $cred['ammesso'];
    $prezzo_acq = $cred['prezzo_acq'];
    $data_chius_table = mysql2table($cred['data_chius']);
    if($cred['caus_chius']!=''){$caus_chius = chius2name($cred['caus_chius'],$pdo);}else{$caus_chius='';}
    $credito = $cred['id_cre'];
    $last_inc = $pdo->query("SELECT max(data_inc) FROM incassi_row WHERE id_cre = '$credito'")->fetchColumn();
    $perc_worst = $pdo->query("SELECT azioni_head.perc_worst FROM azioni_row JOIN azioni_head ON azioni_row.id_az = azioni_head.id_az
                              WHERE id_cred = '$credito'")->fetchColumn();
    if($cred['data_chius']<>'0000-00-00'){$data_chius = mysql2table($cred['data_chius']);}else{$data_chius = '';}
    if($last_inc<> ''){$last_incasso = mysql2table($last_inc);}else{$last_incasso='';}

    /*$row_array1=array($cred['id_cre'],$cred['serie'],$nome_soff,$data_acq,prot2name($cred['cedente'],$pdo),
                     $ammesso,$prezzo_acq,prot2name($cred['originator'],$pdo),$cred['stato'],$data_chius, 
                     $caus_chius,$last_incasso,$perc_worst.'%','','','','',
                     '','Acquisto','=SUM(U'.$k.':EJ'.$k.')'); */
    $row_array1=array($cred['id_cre'],$cred['serie'],'','','','','','','','', 
                     '','','','','','','','','Acquisto','=SUM(U'.$k.':EJ'.$k.')');
    for($anno = $annoMin; $anno<=$annoMax; $anno++){
        for($mese = 1; $mese <= 12; $mese++){
            $mese = sprintf("%02s", $mese);
            $inizio = $anno.'-'.$mese.'-01';
            $fine = $anno.'-'.$mese.'-31';
            if(($cred['data_acq']>=$inizio) && ($cred['data_acq']<=$fine)){
                array_push($row_array1,$cred['prezzo_acq']);
            }else{
                array_push($row_array1,'0.00');
            }
        }
    }
    array_push($row_array1,'=SUM(U'.$k.':EJ'.$k.')');
    $ews->fromArray($row_array1, ' ', 'A'.$k);

0 个答案:

没有答案