RT
代码:
protected void btnReport_Click(object sender, EventArgs e) { IList<Member> StudentList = Search(); string data = ExportCSV(StudentList); string temp = string.Format("attachment;filename={0}", DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv"); Response.ClearHeaders(); Response.AppendHeader("Content-disposition", temp); Response.HeaderEncoding = Encoding.GetEncoding("shift-jis"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = Encoding.GetEncoding("shift-jis"); Response.Write(data); Response.Flush(); Response.Close(); IsReport = false; } public string ExportCSV(IList<Member> list) { string data = ""; data = "First name,Last name,DOB,Email,Phone number,Gender,Points,Rank,Address,Suburb,Postcode,School,Avatar Name,Username,Password,DataCreated,Activated status\n"; foreach (Member tb in list) { data += "\"" + tb.FirstName + "\"," + "\"" + tb.LastName + "\"," + "\"" + tb.DateOfBirth + "\"," + "\"" + tb.Email + "\"," + "\"" + tb.PhoneNumber + "\"," + "\"" + (tb.Gender == "F" ? "Female" : (tb.Gender == "M" ? "Male" : "")) + "\"," + "\"" + tb.Points + "\"," + "\"" + (tb.Rank == null ? "" : tb.Rank.Name) + "\"," + "\"" + tb.Address + "\"," + "\"" + tb.Suburb + "\"," + "\"" + tb.Postcode + "\"," + "\"" + (tb.School == null ? "" : tb.School.Name) + "\"," + "\"" + tb.Nickname + "\"," + "\"" + tb.Username + "\"," + "\"" + tb.Password + "\"," + "\"" + tb.DateCreated + "\"," + "\"" + tb.IsActive + "\"," + "\n"; } return data; }
foreach循环里面用StringBuilder拼装字符串
StringBuilder很好用 还省内存
数据库的话用DATAREADER一条条的写进WEB页面中..不要写进LIST中
没用到Datareader,用的Nhibernate。
在web.config中设置一下请求超时设置:
<httpRuntime executionTimeout="300"/>
单位是秒,默认是110秒。
非常感谢~已经设置过了,虽说能起作用,但是还是时间超长。
<httpRuntime executionTimeout="90" maxRequestLength="800000" useFullyQualifiedRedirectUrl="false"/>
maxRequestLength:设置最大请求的大小,单位:KB
executionTimeout:设置asp.net超时时间,单位:S(秒)
非常感谢~同1楼。