Read php file using php file and excel export

女生的网名这么多〃 提交于 2020-01-06 21:52:52

问题


I have two files like below.

What I'm trying to do is, I want to fetch the second file content in first file, and then export it to xls.

What is the problem with the below code.

My intension is to - read a second php file using first php file, and then excel export that content into .xls in C:/myfiles/test.xls

index.php

    <?php
    $read_file = readfile("my_export_file.php");
    $file = 'test.xls';
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$file");
    echo $read_file;
    ?>

my_export_file.php

    <script type="text/javascript"> my javascript content</script>
    <?php
    include 'db.php';
    $my_query = "mysql query to get the table content";
    ?>
    <table>
    <tr><td>.. mysql row content ..</td><td>.. mysql row content ..</td></tr>
    <tr><td>.. mysql row content ..</td><td>.. mysql row content ..</td></tr>
    <tr><td>.. mysql row content ..</td><td>.. mysql row content ..</td></tr>
    </table>

Could someone help me to get this done.

Thanks in advance.

Regards, Kimz


回答1:


Found out solution, try this:

index.php

<?php
ob_start();
include "my_export_file.php";
$contents = ob_get_contents();
ob_end_clean();
echo $contents; //get whole content/test
?>

Hope this help you.




回答2:


In your export.php file you should fetch result & create table with results. Then just echo that table as given in example.

my_export_file.php

    <?php
    include 'db.php';
    $my_query = "mysql query to get the table content";

    $html = "<table>"
    $html .= "<tr><td>.. mysql row content ..</td><td>.. mysql row content ..</td></tr>";
    $html .= "<tr><td>.. mysql row content ..</td><td>.. mysql row content ..</td></tr>";
    $html .= "<tr><td>.. mysql row content ..</td><td>.. mysql row content ..</td></tr>";
    $html = ."</table>";
    ?>

index.php

    <?php
    require_once("my_export_file.php");
    $file = 'test.xls';
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$file");
    echo $html;
    ?>


来源:https://stackoverflow.com/questions/26056189/read-php-file-using-php-file-and-excel-export

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!