我想在asp.net中使用带有ajax的BackgroundWorker来从数据库中获取数据并将其导出到excel中。当点击开始按钮时,后台工作人员将开始从数据库加载数据,并在excel将下载的流程完成时写入excel。这些过程通过进度条显示使用。
但httpcontext中的问题(可能是错误的),当单击开始按钮时,会创建excel文件,但不会下载。请解决问题。 我给了代码
protected void Page_Load(object sender, EventArgs e)
{
_backgroundWorker.WorkerReportsProgress=true;
_backgroundWorker.WorkerSupportsCancellation = true;
_backgroundWorker.DoWork += new DoWorkEventHandler(_backgroundWorker_DoWork);
_backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(_backgroundWorker_ProgressChanged);
_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_backgroundWorker_RunWorkerCompleted);
}
protected void btnStart_Click(object sender, EventArgs e)
{
if (!_backgroundWorker.IsBusy)
{
_backgroundWorker.RunWorkerAsync();
}
}
void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string display = "Message Pop-up!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
}
void _backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.SetProgress(e.ProgressPercentage);
}
void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Start(ProgressBar1.Progress, _httpContext);
}
private void Start(Progress progress, HttpContext context)
{
ProductBussiness productBussiness = new ProductBussiness();
DataTable dataTable = new DataTable();
StringBuilder builder = new StringBuilder();
Table htmltable=new Table();
int Totalcount = productBussiness.GetCount();
int page = Totalcount / 100;
double progresPercent = 100.00 / page;
for (int i = 1; i <=page; i++)
{
dataTable = productBussiness.GetProductData(i);
int percent=(int)(i * progresPercent);
_backgroundWorker.ReportProgress(percent, progress);
progress.SetProgress((i * progresPercent)/100);
if(i<=page)
ConvertToHtmlTable(dataTable, htmltable, builder, false, context);
else
ConvertToHtmlTable(dataTable, htmltable, builder, true, context);
}
}
#endregion
public StringBuilder ConvertToHtmlTable(DataTable dt, Table table, StringBuilder builder, bool end, HttpContext context)
{
if (dt.Rows.Count == 0)
{
return null;//`enter code here`
}
if (table != null)
{
foreach (DataRow r in dt.Rows)
{
builder.Append("<tr align='left' valign='top'>");
foreach (DataColumn c in dt.Columns)
{
builder.Append("<td align='left' valign='top'>");
builder.Append(r[c.ColumnName]);
builder.Append("</td>");
}
builder.Append("</tr>");
}
}
if (end)
{
context.Response.Clear();
StringBuilder str1 = new StringBuilder();
str1.Append("<html>");
str1.Append("<head>");
str1.Append("<title>");
str1.Append("Page-");
str1.Append(Guid.NewGuid().ToString());
str1.Append("</title>");
str1.Append("</head>");
str1.Append("<body>");
str1.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
str1.Append("style='border: solid 1px Silver; font-size: x-small;'>");
str1.Append("<th align='left' valign='top'>");
foreach (DataColumn c in dt.Columns)
{
str1.Append("<td align='left' valign='top'><b>");
str1.Append(c.ColumnName);
str1.Append("</b></td>");
}
str1.Append("</tr>");
str1.Append(builder);
str1.Append("</table>");
str1.Append("</body>");
str1.Append("</html>");
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.BufferOutput = true;
context.Response.AddHeader("content-disposition", "attachment;filename=ExportData1.xls");
context.Response.Charset = "";
context.Response.ContentType = "application/vnd.xls";
context.Response.Write(str1);
// Response.Flush();
// Response.Close();
context.Response.End();
}
return builder;
}
谢谢你
Pandiyan