PHP generate Excel spreadsheet or Open Office spreadsheet easily

Generating a basic excel spreadsheet easily with PHP.

    header('Pragma: public');
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");                  // Date in the past   
    header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');     // HTTP/1.1
    header('Cache-Control: pre-check=0, post-check=0, max-age=0');    // HTTP/1.1
    header("Pragma: no-cache");
    header("Expires: 0");
    header('Content-Transfer-Encoding: none');
    header('Content-Type: application/vnd.ms-excel;');  
    header("Content-type: application/x-msexcel");
    header('Content-Disposition: attachment; filename="ExcelSpreadsheet.html"');

// print out a normal html table here. eg:

echo '<table><tr><td>spreadsheet content</td><td>goes here</td></tr></table>';

If you use a .xls extension in the script header above, then open office doesn’t seem to open it correctly, it treats it as a text csv import which fails hardcore.

Leave a Reply

Your email address will not be published. Required fields are marked *