【问题标题】:Read php file using php file and excel export使用php文件和excel导出读取php文件
【发布时间】:2014-09-26 09:22:27
【问题描述】:

我有两个如下文件。

我想要做的是,我想在第一个文件中获取第二个文件内容,然后将其导出到 xls。

下面的代码有什么问题。

我的意图是 - 使用第一个 php 文件读取第二个 php 文件,然后 excel 将该内容导出到 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>

有人可以帮我完成这项工作吗?

提前致谢。

问候, 金兹

【问题讨论】:

  • 您应该在 my_export_file.php 中回显您的 html,然后将其包含在 index.php 中而不是 readfile 中
  • @rajeshujade - 你可以添加你的想法/cmets,这样它会更有帮助。
  • 添加了答案。希望对您有所帮助。
  • 与PHPExcel无关

标签: php html forms export-to-excel


【解决方案1】:

找到解决方案,试试这个:

index.php

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

希望对您有所帮助。

【讨论】:

  • excel 导出?如何将 $contents 附加到 excel 导出。?
  • 在“echo $contents;”之前添加以下代码: header("内容类型: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$file");
【解决方案2】:

在您的 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;
    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多