PHP Append to Excel file
<p>I'm having a little bit of trouble trying to append data to an Excel file from PHP. I'm using PHPExcel and the code is below.</p>

<p>The problem is: When the files were small enough it was working great. But then the files started to get bigger and I started to get the Allowed memory size exhausted error. So, i though that instead of writing the file from scratch everytime I'd get only the new data and append to the file. But it's not writing to the file for some reason.</p>

<p>Does anyone have an idea?</p>

<pre><code>function get_subscriptions($process, $filename){      
    /** Error reporting */
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');
    //define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '&lt;br /&gt;');

    /** Include PHPExcel */
    require_once dirname(__FILE__) . '/PHPExcel.php';
    require_once dirname(__FILE__) . '/PHPExcel/IOFactory.php';

    // Create new PHPExcel object
    //$objPHPExcel = new PHPExcel();

    $objPHPExcel = PHPExcel_IOFactory::load(dirname(__FILE__) . "/".$filename);
    $objPHPExcel-&gt;setActiveSheetIndex(0);
    $objSheet = $objPHPExcel-&gt;getActiveSheet();
    $last_line = $objSheet-&gt;getHighestRow()+1;
    $last_id = $objSheet-&gt;getCellByColumnAndRow(0, $last_line)-&gt;getCalculatedValue();

    // Set document properties
    $objPHPExcel-&gt;getProperties()-&gt;setCreator("FHGV")
        -&gt;setLastModifiedBy("FHGV")
        -&gt;setTitle("Report")
        -&gt;setSubject("PSS Report")

    $objPHPExcel-&gt;setActiveSheetIndex(0)
        -&gt;setCellValue('A1', 'ID')
        -&gt;setCellValue('B1', 'Date')
        -&gt;setCellValue('C1', 'Name')
        -&gt;setCellValue('D1', 'CPF')
        -&gt;setCellValue('E1', 'RG')
        -&gt;setCellValue('F1', 'ZIP')
        -&gt;setCellValue('G1', 'Address')
        -&gt;setCellValue('H1', 'No')
        -&gt;setCellValue('I1', 'Reference')
        -&gt;setCellValue('J1', 'City')
        -&gt;setCellValue('K1', 'State')
        -&gt;setCellValue('L1', 'Other')
        -&gt;setCellValue('M1', 'Email')
        -&gt;setCellValue('N1', 'Phone')
        -&gt;setCellValue('O1', 'Cell Phone')
        -&gt;setCellValue('P1', 'Birth Date')
        -&gt;setCellValue('Q1', 'Company')
        -&gt;setCellValue('R1', 'Position')

    global $wpdb;
    $array = $wpdb-&gt;get_results("SELECT id, date_added as 'date',
        (select value from wp_iphorm_form_entry_data dados where element_id = 1 and entry_id = id) as 'name'
        ...
    FROM wp_iphorm_form_entries subscription
    WHERE subscription.post_title = \"".$process."\" AND id&gt;".$last_id);

    $row = $last_line+1; $col = 0;
    foreach($array as $data){
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;id);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;date);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;name);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;cpf);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;rg);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;zip);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;address);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;no);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;reference);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;city);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;state);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;others);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, strip_tags($data-&gt;email));
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;phone);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;cell_phone);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;birth_date);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;company);
        $objSheet-&gt;setCellValueByColumnAndRow($col++, $row, $data-&gt;position);  
        $col=0;$row++;
    }

    for($col = 'A'; $col !== 'S'; $col++) {
        $objPHPExcel-&gt;getActiveSheet()
            -&gt;getColumnDimension($col)
            -&gt;setAutoSize(true);
    }

    // Rename worksheet
    $objSheet-&gt;setTitle('Subscriptions');

    // Save Excel 2007 file
    $callStartTime = microtime(true);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

    // Renaming the file and saving it
    $objWriter-&gt;save(str_replace('functions.php', $filename , __FILE__));
    $callEndTime = microtime(true);
    $callTime = $callEndTime - $callStartTime;
}
</code></pre>