简体   繁体   中英

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. 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.

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. 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.

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. 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. All settings of that library (also caching settings) are stored in static properties of PHPExcel_Settings class. 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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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