How to download data to excel file?

by 4 replies
5
Hello everyone! How do you write a PHP function in codeigniter that lets user download data to an Excel file?


Thank You.
#programming #data #download #excel #file
  • first you download codeigniter and connect it to the database and output the code in the format you require.
  • You could do this?
    • [1] reply
    • I'm not sure exactly what you are trying to do, but you can filter the data into .csv format using php and then dynamically create a file on the server that can be downloaded.
  • Code:
    <?PHP function cleanData(&) 
    {
      = preg_replace("/t/", "t", );
      = preg_replace("/r?n/", "n", );
     if(strstr(, '"'))  = '"' . str_replace('"', '""', ) . '"';
    } 
    # filename for download
     = "website_data_" . date('Ymd') . ".xls";
    header("Content-Disposition: attachment; filename=""");
    header("Content-Type: application/vnd.ms-excel"); 
    0 = false; 
    foreach( as ) 
    { 
    if(!0) 
    {
     # display field/column names as first row 
     echo implode("t", array_keys()) . "rn";
     0 = true;
    } 
    array_walk(, 'cleanData'); 
    echo implode("t", array_values()) . "rn"; 
    }
     exit;
    ?>
    • [ 1 ] Thanks
  • Banned
    [DELETED]

Next Topics on Trending Feed