gpt4 book ai didi

php - 为什么for循环处理完后会出现进度条

转载 作者:行者123 更新时间:2023-11-28 09:30:31 24 4
gpt4 key购买 nike

下面是我的脚本,我在其中放置了进度条来告诉用户正在处理的工作表数据的进度,问题是进度条在将 xls 保存到 csv 的整个过程完成后出现,请告诉我如何操作我可以修改以下内容,以便在处理当前工作表时进度条正确显示。谢谢,

 $excel = new Spreadsheet_Excel_Reader();
$excel->setOutputEncoding('UTF-16');
$excel->read('Student2.xls');
$x=1;
$sep = ",";

$nbSheets = count($excel->sheets);
echo $x." - ".$nbSheets;

$total = $nbSheets - 1;

for($i = 0; $i < $nbSheets; $i++) {


// Calculate the percentation
$percent = intval($i/$total * 100)."%";

// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
</script>';

// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);

// Send output to browser immediately
flush();
//sleep(1);

ob_start();
while($x<=$excel->sheets[$i]['numRows']) {
$y=1;
$row="";
while($y<=$excel->sheets[$i]['numCols']) {
$cell = isset($excel->sheets[$i]['cells'][$x][$y]) ? $excel->sheets[$i]['cells'][$x][$y] : '';
$row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\"";
$y++;
}
echo $row."\n";
$x++;
}
$x = 1; $y = 1;
$fp = fopen("data.csv",'a');
fwrite($fp,ob_get_contents());
fclose($fp);
ob_end_clean();

}

echo "CSV file created successfully";
// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';

最佳答案

也许这可以解决您的问题:

questions#6556790 : echo-string-while-every-long-loop-iteration-flush-not-working

另外,为什么要使用输出缓冲区写入 data.csv 文件?正如它所说,输出缓冲区用于输出数据,因此在这种情况下,我认为简单地在 fwrite 中传递 $row."\n" 会更符合逻辑。功能。如果我在这里遗漏了什么,请纠正我。

关于php - 为什么for循环处理完后会出现进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13785783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com