GridView如何将分页数据全部导出为EXCEL?(附我的代码)
我已实现将GridView导出至Excel,但由于我的Gridview是分页的,所以导出Excel时只能导出当前页的数据,我已在后台设置了
GridView1.AllowPaging = false;
但这句话好像没起作用。现贴上代码,请各高手指教!
前台有一个GridView控件,和Button控件。
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="serialID" DataSourceID="SqlDataSource2">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出至Excel" />
后台代码为:
protected void Button1_Click(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=ShipExcelFile.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false; //就是这句话,怎么不起作用那?
GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("class", "text");
}
}