Read php file using php file and excel export

2019-09-06 08:58发布

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

2条回答
不美不萌又怎样
2楼-- · 2019-09-06 09:07

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.

查看更多
倾城 Initia
3楼-- · 2019-09-06 09:09

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;
    ?>
查看更多
登录 后发表回答