1

以下是我的脚本,我在其中放置进度条来告诉用户正在处理的工作表数据的进度问题是进度条出现在将 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>';
4

3 回答 3

0

我查看了您的代码和您提到的链接。您的代码有一个不同之处,不同之处在于您注释了sleep(1);,并且由于仅此命令,您无法正确看到工作进度条。

所以请取消注释sleep(1);,您的代码将正常工作。

于 2012-12-09T10:17:47.750 回答
0

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

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

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

于 2012-12-09T09:00:58.847 回答
0

您需要使用 AJAX 或某种客户端-服务器通信框架来实现这一点。

您可以尝试查看此示例代码(通过文章底部附近的链接下载代码)。

这个 SO questionthis question似乎解决了类似的问题。

尽管我不想这么说,但搜索“ajax php 进度条”可能会出现大量示例。

于 2012-12-09T10:06:39.650 回答