我有一个GridView,我打算将其导出到.xls文件中。在此gridview中启用了分页。我目前使用的代码只能导出gridview的第一页。
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=DataTable.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//As you notice, below I tried to disable the paging yet it's unsuccessful
//FYI I am able to really prevent first column, header row, and footer row to
//be exported through this
gvGridView.Columns[0].Visible = false;
gvGridView.HeaderRow.Visible = false;
gvGridView.FooterRow.Visible = false;
gvGridView.AllowPaging = false;
for (int i = 0; i < gvGridView.Rows.Count; i++)
{
gvGridView.Rows[i].Attributes.Add("class", "textmode");
}
gvGridView.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
我注意到之前曾问过这个问题,但没有正确答案。
感谢任何帮助。
感谢的
答案 0 :(得分:1)
您应该在指定GridView
之后重新绑定gvGridView.AllowPaging = false;
,然后导出。否则,.RenderControl(hw);
将仅呈现当前选定的GridView
页面。
答案 1 :(得分:1)
在更改AllowPaging属性后,只需创建一个GridView.DataBind()。