导出大清单<>超越c# MVC
本文关键字:超越 MVC | 更新日期: 2023-09-27 18:03:06
我想使用MVC5 c#导出一个超过10000行的列表到excel。
- 当我使用
DataGrid
导出时,excel文件中没有显示一些列- 3列在excel文件中消失,但所有10000行都出现了。
string filename = Guid.NewGuid() + ".xls";
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
List registrationsetList = db.RegistrationSet.Where(x => x.QuestionId == TheQuestion.Id).ToList();
dgGrid.DataSource = registrationsetList;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
Response.End();
之前- 如果我改变
对DataGrid dgGrid = new DataGrid()
有了600行的列表,excel的所有列看起来都很完美。问题是当我导出所有10000行列表时,excel文件卡住了-我没有响应消息。为什么?GridView dgGrid = new GridView();
不要使用DataGrid(),而是使用WebControl GridView():
string filename = Guid.NewGuid() + ".xls";
var gv = new System.Web.UI.WebControls.GridView();
var registrationsetList = db.RegistrationSet.Where(x => x.QuestionId == TheQuestion.Id);
g.DataSource = registrationsetList;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWiter sw = new StringWriter();
var htw = new System.Web.UI.HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
问题(与卡住的excel文件)解决了DataGrid
。问题是,DateGrid
不显示列表中的CLASS中的可空字段列,我绑定到GridView
。