PHPExcel在生成excel文件时弹出进度条或等待图标

PHPExcel pop up progress bar or waiting icon while generating excel file

本文关键字:图标 等待 excel 文件 PHPExcel      更新时间:2023-09-26

我使用PHPExcel生成excel文件,但由于某些excel文件相当大,生成时间较长。

当excel文件生成时,我希望添加一个弹出窗口,显示进度条或等待图标。

我已经尝试了我在谷歌上找到的所有解决方案,但仍然无法完成。我很感激你的帮助。

$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();

在这行代码中开始生成excel:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

没有办法让这个工作的方式,你是建议。首先,您正在创建的标题将不允许向用户显示html。此外,php还没有完成处理,所以在脚本完成之前无法返回用户。

另一个选项是从Javascript异步调用PHP脚本。然后将文件名返回给javascript,并将用户重定向到Excel文件。然后你可以在网页上向用户展示你需要的任何东西。下面是一个非常非常简单的例子。但它可以让你开始。

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!!!");
    }
});

再次,这是非常粗略的,但我只是想让你有一个选择,以进一步追求。

由于这是一篇老文章,我将保持简短。

如果你在你的页面上放置一个iFrame,并将PHPExcel脚本加载到其中,并在某些点放置echo来通知正在发生的事情,你可以模拟这个。另一种选择是加载PHP_Excel脚本作为它自己的页面,上面的回显在适当的位置,并有一个继续或返回按钮,显示脚本完成时。