簡體   English   中英

使用php文件和excel導出讀取php文件

[英]Read php file using php file and excel export

我有兩個如下文件。

我想做的是,我想在第一個文件中獲取第二個文件的內容,然后將其導出到xls。

以下代碼有什么問題。

我的意圖是-使用第一個php文件讀取第二個php文件,然后將內容導出到C:/myfiles/test.xls中的.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>

有人可以幫我完成這項工作。

提前致謝。

問候,金茲

在您的export.php文件中,您應該獲取結果並創建帶有結果的表。 然后按照示例所示回顯該表。

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;
    ?>

找到解決方案,請嘗試以下操作:

index.php

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

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM