2014-09-26 65 views
0

我有两个文件,如下所示。使用php文件和excel导出读取php文件

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

下面的代码有什么问题。

我的内涵是 - 读取使用的第一个PHP文件中的第二个PHP文件,然后Excel导出的内容添加到的.xls在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> 

有人能帮助我完成这件事。

在此先感谢。

问候, Kimz

+0

你应该在my_export_file.php中回显你的html,然后把它包含在index.php而不是readfile中 – 2014-09-26 09:25:27

+0

@rajeshujade - 你可以添加你的想法/评论,这样会更有帮助。 – user3350885 2014-09-26 09:40:58

+0

已添加答案。希望它会帮助你。 – 2014-09-26 09:48:02

回答

1

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

的index.php

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

希望这有助于你。

+0

excel导出?如何将$内容附加到excel导出。 – user3350885 2014-09-26 10:10:09

+0

在“echo $ contents;”之前添加以下代码:header(“Content-type:application/vnd.ms-excel”); header(“Content-Disposition:attachment; filename = $ file”); – 2014-09-26 11:27:13

+0

excellent.this是我想要的。 – user3350885 2014-09-26 11:51:43

1

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