简体   繁体   English

使用PHPExcel Bundle for Symfony2启用单元缓存

[英]Enable cell caching with PHPExcel Bundle for Symfony2

I have the following code which is used to download an Excel file (.xls) file made up of data from a database. 我有以下代码,用于从数据库中下载由数据组成的Excel文件(.xls)文件。 The data can be very large, and at the moment there are 20 columns with 36,000+ records which should be passed on to the spreadsheet. 数据可能非常大,目前有20列包含36,000多个记录,这些记录应传递到电子表格中。

The problem is that the script is running out of memory. 问题在于脚本的内存不足。 Currently the PHP memory limit is set to 128M globally but for this specific script I've set it to 1024M and this prevents the script from hitting the memory limit. 当前,PHP全局内存限制设置为128M,但是对于此特定脚本,我将其设置为1024M,这可以防止脚本达到内存限制。 Because of costs I not able to go absolutely mad with server RAM so I want to be efficient with the RAM I have on the server. 由于成本问题,我不能完全为服务器RAM烦恼,所以我想提高服务器上的RAM的效率。

I know PHPExcel allows you to cache the cells, as well as other performance enhancements, but I'm not able to utilise this with the bundle. 我知道PHPExcel允许您缓存单元以及其他性能增强功能,但是我无法在捆绑包中使用它。 Does anyone know how to do it? 有人知道怎么做吗?

The code I'm using is here: 我正在使用的代码在这里:

    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; 

Bundle is just a wrapper around original PHPExcel library. 捆绑包只是原始PHPExcel库的包装。 All settings of that library (also caching settings) are stored in static properties of PHPExcel_Settings class. 该库的所有设置(以及缓存设置)都存储在PHPExcel_Settings类的静态属性中。 So to set cache you can simply use the following code: 因此,要设置缓存,您只需使用以下代码即可:

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize ' => '256MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

Or if you want memcache: 或者,如果您想要内存缓存:

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache; 
$cacheSettings = array( 'memcacheServer' => 'localhost', 'memcachePort' => 11211, 'cacheTime' => 600 );

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

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