GridView导出Excel
1.前端页面代码:
<asp:Button ID="Button4" runat="server" Text="导出Excel" OnClick="ExcelClick" Height="22px" Width="82px" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="White" BackColor="#111111"
DataKeyNames="ID" CssClass="auto-style2" OnRowDataBound="GridView1_RowDataBound" AllowPaging="True" OnRowCreated="GridView1_RowCreated" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10000" GridLines="Both" Font-Italic="False">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="#222222" />
<Columns>
<asp:BoundField DataField="soDate" HeaderText="销售单日期" ControlStyle-Width="120px" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="False" ReadOnly="True"><ControlStyle Width="120px"></ControlStyle></asp:BoundField>
<%--省略-->
</Columns>
<RowStyle ForeColor="#FFFFFF" BackColor="#222222" />
<SelectedRowStyle BackColor="#AA0000" Font-Bold="True" ForeColor="#FFFF00" />
<PagerStyle BackColor="#222222" ForeColor="#FFFFFF" />
<HeaderStyle BackColor="#111111" Font-Bold="True" ForeColor="#FFFFFF" HorizontalAlign="Center" />
</asp:GridView>
说明:ForeColor为前景色(这里为字体的颜色),BackColor为背景色。第一行是导出Excel的按钮,OnClick="ExcelClick"
调用了下面的导出方法
2.后端代码
//导出GridView Excel
protected void ExcelClick(object sender, EventArgs e)
{
string attachment = "attachment; filename=MyExcel" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", attachment);
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//如果不加这个重载方法导出会报错
}
代码说明:该导出方法就是直接把html的格式一起导出了。
3.效果
浏览器的样式:
导出后用Excel打开:
ASPxGridView导出Excel
1.前端代码
<asp:Button ID="Button4" runat="server" Text="导出Excel" OnClick="ExcelClick1" Height="22px" Width="82px" />
<dx:ASPxGridViewExporter ID="ASPxGridViewExporter1" runat="server" GridViewID="ASPxGridView1" ></dx:ASPxGridViewExporter>
<dx:ASPxGridView ID="ASPxGridView1" ForeColor="White" runat="server" KeyFieldName="id" AutoGenerateColumns="False" >
<Columns>
<dx:GridViewBandColumn Caption="销售单日期" VisibleIndex="2"><Columns><dx:GridViewDataTextColumn Caption="SO issue date" FieldName="soDate" VisibleIndex="0"></dx:GridViewDataTextColumn></Columns></dx:GridViewBandColumn>
<%--省略-->
</Columns>
<SettingsPager PageSize="2000">
</SettingsPager>
<SettingsText EmptyDataRow=" " />
<Styles>
<Table BackColor="#222222"></Table>
<Header BackColor="#222222" ForeColor="White"></Header>
<Row ForeColor="White"></Row>
<PagerBottomPanel BackColor="#222222"></PagerBottomPanel>
</Styles>
<StylesPager>
<PageNumber BackColor="#222222" ForeColor="White"></PageNumber><Pager ForeColor="White"></Pager>
</StylesPager>
</dx:ASPxGridView>
说明:先对dx:ASPxGridView
绑定数据,设置样式,设置表头等元素,然后dx:ASPxGridViewExporter
是导出Excel的方法。
2.后台代码
//导出ASPxGridView Excel
protected void ExcelClick1(object sender, EventArgs e)
{
ASPxGridView1.DataSource = Session["conindevelopment"];
ASPxGridView1.DataBind();
ASPxGridViewExporter1.WriteXlsToResponse("MyExcel" + System.DateTime.Now.ToString("yyyyMMddHHmmss"));
}
说明:Session["conindevelopment"]
是在系统连接数据库查找数据时,把连接数据库的属性和数据一并放入Session中,以此减少系统的性能开销,在导出excel文件往往是一键导出,无需等待。
Session设置代码如下:
private void BindData()
{
SqlConnection sqlConna = new SqlConnection(strConnn);
string strSql = "select * from xxx";
SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConna);
DataSet dt = new DataSet();
sqlDa.Fill(dt, "xxx");
this.ASPxGridView1.DataSource = dt;
this.ASPxGridView1.DataBind();
Session["conindevelopment"] = dt; //设置Session
panelGone.Visible = false;
sqlConna.Close();
sqlConna.Dispose();
}
3.效果
结语:这两种导出方法,我更倾向于第二种,第二种导出来的是真正的Excel表格,而第一种导出的是html网页表格,Excel可以打开,但是存在兼容性问题。当然了,导出Excel的方法有很多种,我只是简单的介绍一下这两种,其他的日后有机会再研究一下。