0

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

4
  • You should echo your html in my_export_file.php & then just include it in index.php instead of readfile Commented Sep 26, 2014 at 9:25
  • @rajeshujade - could u add your thoughts/comments so that it will be more helpful. Commented Sep 26, 2014 at 9:40
  • Added answer. Hopefully it will help you. Commented Sep 26, 2014 at 9:48
  • Nothing to do with PHPExcel Commented Sep 26, 2014 at 13:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

excel export ? how to attach the $contents to excel export.?
add your following code before "echo $contents;" : header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$file");
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.