建立一个WEB工程,添加新项->HTML页面,命名为ProgressBar.htm,内容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="mainWindow">
<head>
<title>无标题页</title>
<script language="javascript">
function SetPorgressBar(pos)
{
//设置进度条居中
var screenHeight = window["mainWindow"].offsetHeight
var screenWidth = window["mainWindow"].offsetWidth
ProgressBarSide.style.width = Math.round(screenWidth / 2)
ProgressBarSide.style.left = Math.round(screenWidth / 4)
ProgressBarSide.style.top = Math.round(screenHeight / 2)
ProgressBarSide.style.height = "21px"
ProgressBarSide.style.display = ""
//设置进度条百分比
ProgressBar.style.width = pos + "%"
ProgressText.innerHTML = pos + "%"
}
//完成后隐藏进度条
function SetCompleted()
{
ProgressBarSide.style.display = "none"
}
</script>
</head>
<body>
<div id="ProgressBarSide" style="position:absoluteheight:21xwidth:100pxcolor:Silverborder-width:1pxborder-style:Soliddisplay:none">
<div id="ProgressBar" style="position:absoluteheight:21pxwidth:0%background-color:#3366FF"></div>
<div id="ProgressText" style="position:absoluteheight:21pxwidth:100%text-align:center"></div>
</div>
</body>
</html>
后台代码,Default.aspx.cs:
using System
sing System.Data
sing System.Configuration
sing System.Web
sing System.Web.Security
sing System.Web.UI
sing System.Web.UI.WebControls
sing System.Web.UI.WebControls.WebParts
sing System.Web.UI.HtmlControls
sing System.Threading
sing System.IO
ublic partial class _Default : System.Web.UI.Page
private void beginProgress()
{
//根据ProgressBar.htm显示进度条界面
string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm")
StreamReader reader = new StreamReader(@templateFileName,System.Text.Encoding.GetEncoding("GB2312"))
string html = reader.ReadToEnd()
reader.Close()
Response.Write(html)
Response.Flush()
}
private void setProgress(int percent)
{
string jsBlock = "<script>SetPorgressBar('" + percent.ToString() + "')</script>"
Response.Write(jsBlock)
Response.Flush()
}
private void finishProgress()
{
string jsBlock = "<script>SetCompleted()</script>"
Response.Write(jsBlock)
Response.Flush()
}
private void Page_Load(object sender, System.EventArgs e)
{
beginProgress()
for (int i = 1i <= 100i++)
{
setProgress(i)
//此处用线程休眠代替实际的 *** 作,如加载数据等
System.Threading.Thread.Sleep(50)
}
finishProgress()
}
很简单,因为数据读到集合所用的时间远远不如数据库的存储,因此只要计算插入数据库的进度即可。做法是:在读入数据库的时候 根据集合的大小生成一个最大进度为集合长度的进度条,每成功写入数据库一条,进度条 +1。两部分:
POI解析Excel文件是否提供接口可以取得完成的进度(百分数),没有的话也可以弄个假的,处理中每N秒+1%,结束时直接设置为100%,N根据实际处理速度调整,免得最后超过100%...
页面有很多进度条实现,百度一下就有,需要传入进度数作为参数,来展现,自己百度一下:jsProgressBarHandler (Dynamic Javascript Progress。第一个就是。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)