user4433485
user4433485

Reputation:

php table export to Excel

I am trying to export my php table with arrays to Excel, wich I have some difficulities. enter image description here

Once I click on Get date, a datepicker pops up, when the user click on the specefic date he want to check the call history, the code will load all info from that day. it works good, have got it tested on a php table with echo, arrays are fine etc.

But now I try to export the table to a Excel document. it makes me download the file, but when I open it on excel, it shows me what you see on the screenshot on excel.. and not the table with all info. so actually it shows me enter image description here

And I want to export the result of that, the table. as you can see here: enter image description here

the form:

<form action='getlist.php' method='post'> 
    <input type='text' name='exte' class='exte' value="<?php echo $_POST['exte']?>">  
    <input type='text' name='datepick2' class='datepick2' value='Kies een datum!'>
    <input type='submit' name='aanvragen' id='aanvragen' value='aanvragen'>
</form>

See here the headers and stuff

if (isset($_POST['datepick2'])) {
        $date = strtotime($_POST['datepick2']);
        $milliseconds = round($date * 1000);
        $request_url = url.cdr."?realm=".realm."&ext=".$_POST['exte']."&from=".$milliseconds;  // wil je de link boven aan weg halen moet je deze echo ff weg doen.
        $gettoken = substr($_SESSION['token'], 0, -1);
        $ch = curl_init();
        $filename = "website_data_" . date('Ymd') . ".xls";
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Content-Type: application/vnd.ms-excel");       
        $request_headers = array();
        $request_headers[] = 'Content-Type: application/json; charset=utf-8';
        $request_headers[] = 'x-auth-token: '.$gettoken;

        curl_setopt($ch, CURLOPT_URL, $request_url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
        curl_setopt($ch, CURLOPT_HEADER,FALSE);

        $response  = curl_exec($ch);

        $response = json_decode($response, true);

This is what I use for the download:

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

the table:

echo    "<table div id='arraytable'>
                        <thead>
                        <tr>
                        <td>Beller</td>
                        <td>Beller nummer</td>
                        <td>datum</td>
                        <td>ontvanger naam<td>
                        <td>ontvanger nummer</td>
                        <td>Billing seconds</td>
                        <td>Direction</td>




                        </tr>";     

            foreach ($response as $key => $value) {

                echo "
                    <tr>
                    <td>".$value ['caller_id_name']."</td>
                    <td>".$value ['caller_id_number']."</td>
                    <td>".$value ['timestamp']."</td> 
                    <td>".$value ['callee_id_name']."</td>
                    <td>".$value ['callee_id_number']."</td>
                    <td>".$value ['billing_seconds']."</td>
                    <td>".$value ['direction']."</td>
                    ";
            } 

Upvotes: 0

Views: 221

Answers (1)

user4433485
user4433485

Reputation:

I think I did fix it:) after the table code i've put $filename->save('php://output'); seems I was missing this since it does work now, it's quite buggy at the moment, but it works. Now I am going to debug, thanks anyway people:)

Upvotes: 2

Related Questions