1

I using PHPExcel to generate excel file, but due to some excel file is quite big, it takes time to generate.

When excel file is generating, I wish to add a popup that shows either progress bar or a waiting icon.

I've tried all the solution I found on google but still cannot be done. I appreciate all kind of help.

$objPHPExcel = new PHPExcel();<br>
$F=$objPHPExcel->getActiveSheet();

//other's code to generate excel file

header('Content-Type: application/vnd.ms-excel');<br>
header('Content-Disposition: attachment;filename="report.xls"');<br>
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');<br>
$objWriter->save('php://output');<br>
exit();

The excel start to generate in this line of code:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
Elise
  • 65
  • 3
  • 9
  • I actually just built something like this, but like @James said you will need to take an asynchronous approach to update the page without reloading. I'll post below for you. – Jordan Davis Feb 11 '16 at 07:15

2 Answers2

2

There is no way to have this work the way you are proposing. First, the headers you are creating will not allow html to show to the user. Also, the php hasn't finished processing so there is no way to go back to the user until the script is done.

Another option would be to call to the PHP script from Javascript asynchronously. Then return the file's name to the javascript, and redirect the user to the Excel file. Then you can show whatever you need to the user on the web page. Below is a very very very simplified example. But it could get you started.

php:

$objPHPExcel = new PHPExcel();
$F=$objPHPExcel->getActiveSheet();
//Other code to Generate Excel File

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('path/to/excelfile.xls');

//Now return some info to user
//You will want to actually do some testing here
//to make sure the file was created
echo json_encode(array(
    "result" => 200,
    "path" => "path/to/excelfile.xls"
));

jquery:

//Call to the php file above to start the processing
//You can add some spinner or popup message here
$.get( "path/to/phpexcel.php", function( data ) {
    //Now check the return data
    if(data.result === 200){
        //If all is well just redirect the user to the file
        window.location = data.path;
    }else{
        alert("Something went wrong!!!");
    }
});

Again, this is very rough, but I just wanted you to have an option to pursue further.

James
  • 1,562
  • 15
  • 23
0

Since this is an old post, I'll keep it short.

If you place a iFrame on your page and load your PHPExcel script into it with echoes placed at certain points of informing what is happening, you can simulate this. Other option is to load the PHP_Excel script as its' own page with the above echoes in place and a continue or return button that shows when the script is finished.

designdrumm
  • 64
  • 1
  • 4
  • To answer a bit closer to the OP question, you could simulate a load bar this way too. You could echo an img that is each bar on the progress bar. Making sure to make each img float left, clear right and inline so they butt up against each other in a line in the html when echoed. – designdrumm Oct 17 '17 at 06:26