Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:43:08+00:00 2026-05-26T05:43:08+00:00

I am using PHPExcel to create an Excel document, using data from a MySQL

  • 0

I am using PHPExcel to create an Excel document, using data from a MySQL database. My script must execute in under 512MB of RAM, and I am running into trouble as my export reaches 200k records:

PHP Fatal error: Allowed memory size of…

How can I use PHPExcel to create large documents in as little amount of RAM as possible?

My current code:

            // Autoload classes
    ProjectConfiguration::registerPHPExcel();


    $xls = new PHPExcel();
    $xls->setActiveSheetIndex(0);


    $i = 0; 
    $j = 2; 
            // Write the col names
    foreach ($columnas_excel as $columna) {
        $xls->getActiveSheet()->setCellValueByColumnAndRow($i,1,$columna);
        $xls->getActiveSheet()->getColumnDimensionByColumn($i)->setAutoSize(true);
        $i++;
    }


    // paginate the result from database
    $pager = new sfPropelPager('Antecedentes',50);
    $pager->setCriteria($query_personas);
    $pager->init();
    $last_page = $pager->getLastPage();

    //write the data to excel object
    for($pagina =1; $pagina <= $last_page; $pagina++) {
        $pager->setPage($pagina);
        $pager->init();
        foreach ($pager->getResults() as $persona) {
            $i = 0;
            foreach ($columnas_excel as $key_col => $columnas) {
                $xls->getActiveSheet()->setCellValueByColumnAndRow($i,$j,$persona->getByName($key_col, BasePeer::TYPE_PHPNAME));
                $i++;
            }
            $j++;
        }
    }

    // write the file to the disk
    $writer = new PHPExcel_Writer_Excel2007($xls);
    $filename = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . "$cache.listado_personas.xlsx";
    if (file_exists($filename)) {
        unlink($filename);
    }
    $writer->save($filename);

CSV version:

// Write the col names to the file
$columnas_key = array_keys($columnas_excel);
file_put_contents($filename, implode(",", $columnas_excel) . "\n");

//write data to the file
for($pagina =1; $pagina <= $last_page; $pagina++) {                     
        $pager->setPage($pagina);
        $pager->init();
        foreach ($pager->getResults() as $persona) {
            $persona_arr = array();
            // make an array        
            foreach ($columnas_excel as $key_col => $columnas) {
                $persona_arr[] = $persona->getByName($key_col, BasePeer::TYPE_PHPNAME);                 
            }
            // append to the file
            file_put_contents($filename, implode(",", $persona_arr) . "\n", FILE_APPEND | LOCK_EX);             
        }
    }

Still have the problem of RAM when Propel makes requests to the database, it’s like Propel, does not release the RAM every time you make a new request. I even tried to create and delete the Pager object in each iteration

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:43:08+00:00Added an answer on May 26, 2026 at 5:43 am

    Since it appears you can use a CSV, try pulling 1 record at a time and appending it to your CSV. Don’t try to get all 200k records at the same time.

    $cursor = mysql_query( $sqlToFetchData );    // get a MySql resource for your query
    $fileHandle = fopen( 'data.csv', 'a');       // use 'a' for Append mode
    
    while( $row = mysql_fetch_row( $cursor ) ){  // pull your data 1 record at a time
        fputcsv( $fileHandle, $row );            // append the record to the CSV file
    }
    
    fclose( $fileHandle );                       // clean up
    mysql_close( $cursor );
    

    I’m not sure how to transform the CSV into an XLS file, but hopefully this will get you on your way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that will generate a report using PHPExcel from data
I am using PHPExcel to export data from my PHP page to Excel. I
I am using PHPExcel to read data from Excel. It is not giving me
I'm using data from Excel2007 as parsed by PHPExcel , and dates come out
I'm using PHPExcel library for many Excel manipulations, combined with PHP/MySQL. That helps me
I want to read/retrieve an Image from excel file to PHP using PHPExcel. This
I have built an Excel document using the PHPExcel Library. My document opens and
I'm trying to create a multi-sheet excel document, and thus far I'd been doing
1) I have been working on fetching data from an excel sheet i can
I am using a PHP library PHPExcel in my script. Now the library is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.