//点击按钮,完成在服务器上生成文件,并下载到客户端的任务.
protected void btnOutput_Click(object sender, EventArgs e)
{
try
{
fillGridView_emp(this.grd_employee, this.grd_emp_temp);
string strYear = this.lstYear.Text;
string strSeason = this.lstPeriod.Text;
foreach (GridViewRow grdRow in this.grd_employee.Rows)
{
//本想做成一次下载多个选中的对象,但是后来却发现做不了,于是就改成了CheckBox只能选中一个.这样就不会出问题了.
if(((CheckBox)(grdRow.Cells[4]).FindControl("ckb_Output")).Checked == true)
{
string strLocalEmpNo = ((LinkButton)(grdRow.Cells[1].Controls[0])).Text.ToString();
string strLocalEmpName = (grdRow.Cells[2]).Text.ToString();
//write to excel
if (false == writeToExcel(strYear, strSeason, strLocalEmpNo, strLocalEmpName))
{
//error message
}
else
{
//System.GC.Collect();放在这个地方,服务器上的EXCEL进程杀不了,
//若放在下面(蓝色部分)则可以,具体原因不明,有待研究.
string strFileName = Session["fullName"].ToString();
FileDownload(strFileName);
}
break;
}
}
}
catch (System.Exception ex)
{
ExceptionHandler.HandleException(ex);
}
finally
{
System.GC.Collect();
}
}
/// <summary>
/// 在服务器上生成一个包含所需内容的临时文件
/// </summar

这篇博客详细介绍了如何在C# Web应用中,通过按钮事件生成服务器上的临时Excel文件,将文件下载到客户端,然后在服务器端删除临时文件。在btnOutput_Click事件处理中,根据用户选择生成文件,使用Excel COM对象操作模板文件,保存为临时文件,再通过多种下载方法之一供用户下载。最后在Session_End事件中清理临时文件。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



