【发布时间】:2014-01-14 14:47:38
【问题描述】:
我的申请有问题。
首先,对不起我的英语,我来自智利,我还在学习。
我有一种方法可以为 Excel 提供来自数据库的信息。客户不需要在屏幕上看到报告,他们只需要从应用程序下载。
此方法调用存储过程,它提供了太多数据(大约 600 列和 20000 行),因此,当将表分配给 GridView 时,在 DataBind() 上,会抛出 System.OutOfMemoryException。
还有其他方法可以用数据加载 GridView 吗?我无法过滤或简化查询,因为客户需要报告中的每个日期。 我正在使用 Visual Studio 2010、Framework 4.0、SQL Server 2008。
这是方法:
private void getsReport()
{
string reportName = "Globarl_Report_" + User.Identity.Name + "_" + DateTime.Now.ToString("yyyyMMdd")+".xls";
Database db = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["SNAT"].ToString());
DBCommandWrapper com = db.GetSqlStringCommandWrapper("usp_con_global_report);// sp that give report.
com.CommandTimeout = 360;
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
Page pagina = new Page();
HtmlForm form = new HtmlForm();
DataTable tabla = db.ExecuteDataSet(com).Tables[0]; ;
GridView dg = new GridView();
dg.EnableViewState = false;
dg.DataSource = tabla;
dg.DataBind();//THIS LINES THROWS EXCEPTION!!
pagina.EnableEventValidation = false;
pagina.DesignerInitialize();
pagina.Controls.Add(form);
form.Controls.Add(dg);
pagina.RenderControl(htw);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + nombreArchivo);
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.Default;
Response.Write(sb.ToString());
Response.End();
}
感谢您的帮助!!
【问题讨论】:
-
你有什么理由向用户展示 20.000 行吗?您应该添加搜索和过滤以及分页。还有600列???没有客户需要 20.000 行 600 列!
-
是否可以修改
usp_con_global_report以限制您返回的行数? -
或在 google 上查找虚拟化
-
@ScottChamberlain,对,我的错 - msdn.microsoft.com/en-us/library/windows/apps/hh780657.aspx 这解释了虚拟化的概念(尽管它不适用于 asp.net)