62,025
社区成员
发帖
与我相关
我的任务
分享
前台:window.location.href = "../Temp.aspx";
后台: try{
dtSource = Session["MyDataTable"] //假设数据在Session中
if (dtSource == null || dtSource.Rows.Count == 0)
{
return;
}
DataGrid dg = new DataGrid();
dg.DataSource = dtSource;
dg.DataBind();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "UTF-8";//GB2312
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";//text/csv
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
System.IO.StringWriter oSW = new System.IO.StringWriter();
HtmlTextWriter oHW = new HtmlTextWriter(oSW);
dg.RenderControl(oHW);
HttpContext.Current.Response.Write(oSW.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}catch(e){
log.Error(e);
Response.Redirect("原页面.aspx");
}