0

I'm using PHPExcel to generate excel files (crazy right ?) with information I get from a database, the thing is whenever I create the file it is saved in the server and then I just send a link to that file to the user so he can access it.

I doubt this is the right way of doing it, what I'm looking for is to just send the file to the browser without saving it on the server.

How do I output EXCEL file directly to user without storing locally?

Code:

$fileName  = "Request_" . $idRequest . "_Update.xls";
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$objWriter->save($fileName);
$objPHPExcel = PHPExcel_IOFactory::load($fileName);


echo json_encode(array(
        "ExitCode" => 0,
        "Message" => "Success",            
        "data" => $request,
        "File" => "../reports/$fileName"
    ));

Once i receive File in the $.ajax call that receives it just add it to an anchor tag:

$.ajax({
       ...
       success : function(response){
           $('#container').append('<a href="'+response.File+'">Here is your file</a>
       }

3 Answers 3

2

check this out.

//    reset all output buffering
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
    header('Content-type: application/ms-excel');
    header('Content-Disposition:  inline; attachment; filename='.$filename);

    // we can't send any more headers after this
    flush();

    $excel = new PhpExcel();
    $excel->setActiveSheetIndex(0);
    $sheet = $excel->getActiveSheet();
    // in this example, $data was an array in the format row => value
    //  data structure is not relevant to issue
    foreach ($data as $key => $value) {
         // add data to sheet here
         $sheet->SetCellValue('A' . $key, $value);
         // etc...
    }
    $writer = new PHPExcel_Writer($excel);
    // push to browser
    $writer->save('php://output');
Sign up to request clarification or add additional context in comments.

Comments

1

The accepted response to this question shows how to handle a success response be offering the file for download, or a failure response handled by the js (e.g. displayed within the current html page) without mime type issues

3 Comments

Would that answer be compatible across multiple browsers ? I've just used Ajax with jQuery
No guarantees - I'm not a js or browser expert, but it's the best answer I can come up with and looks as though it should work
Thanks I'll definitely try it and I'll let you know
1

You can send a

header("Content-Disposition: attachment; filename=\"file.xls\"");

to force download and then

header("Content-type:application/vnd.ms-excel");

to say the browser that you're sending an excel file and finally just echo the file as an html table:

<?php
header("Content-Disposition: attachment; filename=\"file.xls\"");
header("Content-type:application/vnd.ms-excel");
echo "<html><table>...</table></html>";  // your table data here..
?>

Excel can open .html files with <table>s so there shouldn't be any problem.

3 Comments

Can you be a little more specific please ?
@IsaacGonzalez Does this make sense?
Ok i get it now, but the thing is that since i do some other operations to the content in the file, i am not going to output everything as a <table> without re-doing a lot of my code. +1 for your help

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.