Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to download data to excel file?

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


    Thank You.

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?PHP function cleanData(&$str) 
    {
     $str = preg_replace("/\t/", "\\t", $str);
     $str = preg_replace("/\r?\n/", "\\n", $str);
     if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
    } 
    # filename for download
    $filename = "website_data_" . date('Ymd') . ".xls";
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel"); 
    $flag = false; 
    foreach($data as $row) 
    { 
    if(!$flag) 
    {
     # display field/column names as first row 
     echo implode("\t", array_keys($row)) . "\r\n";
     $flag = true;
    } 
    array_walk($row, 'cleanData'); 
    echo implode("\t", array_values($row)) . "\r\n"; 
    }
     exit;
    ?>
    Last edited by masmedia; 02-26-2014 at 03:08 PM. Reason: Link removal request


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •