当有超过5K行时,将C#报告导出到Excel
本文关键字:报告 Excel 5K 行时 | 更新日期: 2023-09-27 17:59:27
从我在谷歌上看到的一切来看,这似乎是个问题。我有一些代码(发布在下面),它适用于任何较小的报告,但一旦返回了大约5K或更多的记录,它就会拒绝导出。
有人有什么想法吗?由于公司限制,我们不能使用任何非VS2010标准的第三方工具或插件。
我的代码:
就在我运行报告时将数据源绑定到gridview之前,我用数据源填充了一个会话变量:
var adapter = new SqlDataAdapter(cmd2);
var ds = new DataSet();
adapter.Fill(ds, "MyTableName");
// Add this to a session variable so the datagrid won't get NULLed out on repost
Session["SSRptMenu"] = ds;
我这样做是因为一旦它运行完毕,用户可能会也可能不会选择导出它。如果他们选择导出它,使用会话变量重新填充网格视图会更快。
然后,我有一个单独的功能,负责导出报告。我必须重新填充网格视图,所以我使用会话变量:
private void ExportGridView()
{
// Exports the data in the GridView to Excel
// First, fill the datagrid with the results of the session variable
DataSet gridDataSource = (DataSet)Session["SSRptMenu"];
GridView_Reports.Visible = true;
GridView_Reports.DataSource = gridDataSource;
GridView_Reports.DataBind();
// Exports the data in the GridView to Excel
string attachment = "attachment; filename=RingMaster.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView_Reports.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
正如我所说,这在较小的报告中完美地工作,但当你得到大约5K或更多的记录时,只导出一张空白表。
尝试在配置文件中设置<httpRuntime maxRequestLength="1048576"/>
(或其他一些数字以满足您的需要)。默认情况下,maxRequestLength只允许4MB的数据。
检查响应缓冲区限制。默认值仅设置为4MB。
编辑ASP设置(IIS 7)的说明
您可以使用jQuery吗?作为VS2010的标准配置。。。?
jQuery有一个插件,可以通过将文件拆分成更小的块来上传大小不受限制的文件。
链接是https://github.com/blueimp/jQuery-File-Upload
我知道,由于您对第三方代码的使用限制,您不能直接使用它,但您仍然可以查看源代码,也许可以了解如何使用自己的代码来实现这一点。
Please try the code after HttpContext.Current.Response.Write(tw.ToString());
and place your HttpContext.Current.Response.End(); after the catch.
#region " Summary - Excel Upload "
private void fnExcelUpload()
{
try
{
dgDashboard.AllowPaging = false;
dgDashboard.Columns[0].Visible = false;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename = ExcelExport.xls");
Response.Charset = "";
Response.Buffer = true;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
fillDashboard();
dgDashboard.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Flush();
//HttpContext.Current.Response.End();
}
catch (Exception Ex)
{
ErrorLog obj = new ErrorLog(Session["PROGRAMCODE"].ToString(), Ex.Message, Ex.StackTrace, this.Page.ToString(), new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]).HostName.ToString(), Session["EMPNUMBER"].ToString(), HttpContext.Current.User.Identity.Name.ToString());
}
HttpContext.Current.Response.End();
}
protected void imgExcelExport_Click(object sender, ImageClickEventArgs e)
{
fnExcelUpload();
}
#endregion
#region " Summary - Excel Upload "
private void fnExcelUpload()
{
try
{
dgDashboard.AllowPaging = false;
dgDashboard.Columns[0].Visible = false;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename = ExcelExport.xls");
Response.Charset = "";
Response.Buffer = true;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
fillDashboard();
dgDashboard.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Flush();
//HttpContext.Current.Response.End();
}
catch (Exception Ex)
{
ErrorLog obj = new ErrorLog(Session["PROGRAMCODE"].ToString(), Ex.Message, Ex.StackTrace, this.Page.ToString(), new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]).HostName.ToString(), Session["EMPNUMBER"].ToString(), HttpContext.Current.User.Identity.Name.ToString());
}
HttpContext.Current.Response.End();
}
protected void imgExcelExport_Click(object sender, ImageClickEventArgs e)
{
fnExcelUpload();
}
#endregion