export msyql web displayed query to ms excel

Discussion in 'PHP' started by wool01, Mar 8, 2010.

  1. #1
    Hi to all...
    I need help for my script in php.. I have a form that queries certain results and display it in web. My problem is i need to get those web displayed results to download and export it in excel. Can someone help me on this? im not a php programmer and just starting to learn it.. thanks in advance!
     
    wool01, Mar 8, 2010 IP
  2. wool01

    wool01 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    in addition to my first post, i need to put a "download button or link" somewhere on that page so when i press it, it will download the displayed results and export to ms-excel.
     
    wool01, Mar 8, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    Here's one that i made.
    Take note of the line on Bold, this is from the form page (WHERE YOU WANT YOUR BUTTON "download button or link") passing the array $_POST where it is parsed to xls format(excel), this is just basic formatting.
    <?php
    $records = $_POST['records'];

    function cleanData(&$str)
    {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    }

    $filename = "ra_work_units" . date('Ymd') . ".xls";

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");

    $flag = false;
    foreach($records as $row) {
    if(!$flag) {
    echo implode("\t", array_keys($row)) . "\n";
    $flag = true;
    }
    array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\n";
    }
    ?>

    here is the array format
    $_POST = array('dbrow1'=>array('column11','column12','column13'),'dbrow2'=>array('column21','column22','column23'));
     
    bartolay13, Mar 9, 2010 IP
  4. wool01

    wool01 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you for the quick reply... im just a little bit confused..
    1. so i need to create another php file that contains the code that you created right? to be able to call that file for my "download link"
    2.
    $records represents the query results from the form page where i want to put the link button right? so if i have $results variable from the query form then i will change the $records to $results like $results=$_POST['results'];
    3.
    where i will put this code?

    sorry im so dumb that i need to clarify this things..
     
    wool01, Mar 9, 2010 IP
  5. bhoo-day

    bhoo-day Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    hello wool01,

    In order to export data to Excel in php, there is a awesome excel library for php. It is phpexcel. Go to http://www.codeplex.com/PHPExcel to download the library.

    If you download the library, you can see some good examples like what you want.

    Hope it helps
     
    bhoo-day, Mar 10, 2010 IP