简体   繁体   English

PHP表导出到Excel

[英]php table export to Excel

I am trying to export my php table with arrays to Excel, wich I have some difficulities. 我试图将我的带有数组的php表导出到Excel,但有一定难度。 在此处输入图片说明

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. 它工作正常,已经在带有回声的php表上测试了它,数组很好等等。

But now I try to export the table to a Excel document. 但是现在我尝试将表导出到Excel文档。 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. 它使我可以下载文件,但是当我在excel上打开文件时,它显示的是您在excel ..屏幕截图上看到的内容,而不是包含所有信息的表格。 so actually it shows me 所以实际上它告诉我 在此处输入图片说明

And I want to export the result of that, the table. 我想导出结果表。 as you can see 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>
                    ";
            } 

I think I did fix it:) after the table code i've put $filename->save('php://output'); 我想我确实修复了它:)在表代码之后,我放了$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:) 现在我要调试,谢谢大家:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM