我有以下代码,用于下载由数据库中的数据组成的Excel文件(.xls)文件。数据可能非常大,目前有20列,有36,000多条记录,应该传递给电子表格。
问题是脚本内存不足。目前,PHP内存限制全局设置为128M,但对于此特定脚本,我将其设置为1024M,这可以防止脚本达到内存限制。由于成本原因,我无法对服务器RAM非常生气,因此我希望能够提高服务器上的RAM效率。
我知道PHPExcel允许您缓存单元格以及其他性能增强功能,但我无法将其与捆绑包一起使用。有谁知道怎么做?
我正在使用的代码在这里:
ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very larg
ini_set('memory_limit', '1024M');
$enviroFigures = $dm->createQuery('
SELECT efu.billingCustomer, efu.division, efu.customerSite, efu.town, efu.postcode, efu.jobNumber, efu.jobStatus, efu.jobType, efu.completionDate, efu.description, efu.wasteType, efu.ewcCode, efu.container, efu.quantity, efu.disposalMethod, efu.wasteHierarchy, efu.jobNotes, efu.totalUom, efu.co2Saving, efu.customerOrderDate
FROM CoreBundle:EnviroFiguresUpload efu
WHERE efu.division IN (:profile)
ORDER BY efu.id DESC'
)->setParameter('profile', $divisionProfiles);
$enviFig = $enviroFigures->getResult();
$excel = $this->get('phpexcel')->createPHPExcelObject();
$excel->getProperties()->setCreator('iStyle')
->setTitle('Resource Profile Data');
$i1 = 1;
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setTitle('Resource Profile Data')
->setCellValue('A'.$i1, 'Customer')
->setCellValue('B'.$i1, 'Division')
->setCellValue('C'.$i1, 'Customer Site')
->setCellValue('D'.$i1, 'Town')
->setCellValue('E'.$i1, 'Postcode')
->setCellValue('F'.$i1, 'Job Number')
->setCellValue('G'.$i1, 'Job Status')
->setCellValue('H'.$i1, 'Job Type')
->setCellValue('I'.$i1, 'Completion Date')
->setCellValue('J'.$i1, 'Description')
->setCellValue('K'.$i1, 'Waste Type')
->setCellValue('L'.$i1, 'EWC Code')
->setCellValue('M'.$i1, 'Total Collected')
->setCellValue('N'.$i1, 'Total Co2 Saving')
->setCellValue('O'.$i1, 'Container')
->setCellValue('P'.$i1, 'Quantity')
->setCellValue('Q'.$i1, 'Disposal Method')
->setCellValue('R'.$i1, 'Waste Hierarchy')
->setCellValue('S'.$i1, 'Customer Order Date')
->setCellValue('T'.$i1, 'Job Notes');
$i = 2;
for($d = 0; $d < count($enviFig); $d++) {
$excel->getActiveSheet()
->setCellValue('A'.$i, $enviFig[$d]['billingCustomer'])
->setCellValue('B'.$i, $enviFig[$d]['division'])
->setCellValue('C'.$i, $enviFig[$d]['customerSite'])
->setCellValue('D'.$i, $enviFig[$d]['town'])
->setCellValue('E'.$i, $enviFig[$d]['postcode'])
->setCellValue('F'.$i, $enviFig[$d]['jobNumber'])
->setCellValue('G'.$i, $enviFig[$d]['jobStatus'])
->setCellValue('H'.$i, $enviFig[$d]['jobType'])
->setCellValue('I'.$i, $enviFig[$d]['completionDate'])
->setCellValue('J'.$i, $enviFig[$d]['description'])
->setCellValue('K'.$i, $enviFig[$d]['wasteType'])
->setCellValue('L'.$i, $enviFig[$d]['ewcCode'])
->setCellValue('M'.$i, $enviFig[$d]['totalUom'])
->setCellValue('N'.$i, $enviFig[$d]['co2Saving'])
->setCellValue('O'.$i, $enviFig[$d]['container'])
->setCellValue('P'.$i, $enviFig[$d]['quantity'])
->setCellValue('Q'.$i, $enviFig[$d]['disposalMethod'])
->setCellValue('R'.$i, $enviFig[$d]['wasteHierarchy'])
->setCellValue('S'.$i, $enviFig[$d]['customerOrderDate'])
->setCellValue('T'.$i, $enviFig[$d]['jobNotes']);
$i++;
}
// create the writer
$writer = $this->get('phpexcel')->createWriter($excel, 'Excel5');
// create the response
$response = $this->get('phpexcel')->createStreamedResponse($writer);
// adding headers
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
return $response;
答案 0 :(得分:1)
Bundle只是原始PHPExcel库的包装器。
该库的所有设置(也称为缓存设置)都存储在PHPExcel_Settings
类的静态属性中。因此,要设置缓存,您只需使用以下代码:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize ' => '256MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
或者如果你想要memcache:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;
$cacheSettings = array( 'memcacheServer' => 'localhost', 'memcachePort' => 11211, 'cacheTime' => 600 );