欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于GridView和ASPxGridView导出Excel表格的解决方法

程序员文章站 2022-04-04 21:10:01
...
  • 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>   
        <asp:BoundField DataField="RF010" HeaderText="创建日期"  ControlStyle-Width="120px" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="False" ReadOnly="True"><ControlStyle Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF001" HeaderText="单号/办单号" ControlStyle-Width="200px"  ReadOnly="True"><ControlStyle CssClass="auto-style188" Width="200px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF003" HeaderText="单数" ControlStyle-Width="45px"  ControlStyle-CssClass="auto-style182"><ControlStyle CssClass="auto-style182" Width="45px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF004" HeaderText="需求数" ControlStyle-Width="60px" ControlStyle-CssClass="auto-style182" ReadOnly="True"><ControlStyle CssClass="auto-style182" Width="60px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF002" HeaderText="货号" ControlStyle-Width="120px" ControlStyle-CssClass="auto-style188" ReadOnly="True"><ControlStyle CssClass="auto-style188" Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF006" HeaderText="操作员" ControlStyle-Width="55px" ControlStyle-CssClass="auto-style188"><ControlStyle CssClass="auto-style188" Width="55px"></ControlStyle></asp:BoundField>       
        <asp:BoundField DataField="RF005" HeaderText="二手" ControlStyle-Width="55px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="55px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF007" HeaderText="工单类型" ControlStyle-Width="55px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="55px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF008" HeaderText="工序" ControlStyle-Width="55px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="55px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF009" HeaderText="来料情况" ControlStyle-Width="55px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="55px"></ControlStyle></asp:BoundField>      
        <asp:BoundField DataField="StartTime" HeaderText="开始时间" ControlStyle-Width="120px" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="False" ReadOnly="True"><ControlStyle Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="EndTime" HeaderText="结束时间" ControlStyle-Width="120px" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="False" ReadOnly="True"><ControlStyle Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="td203" HeaderText="复期交货" ControlStyle-Width="120px" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="False" ReadOnly="True"><ControlStyle Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="td005" HeaderText="货品名称" ControlStyle-Width="120px" ControlStyle-CssClass="auto-style188" ReadOnly="True"><ControlStyle CssClass="auto-style188" Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="td220" HeaderText="Web Order#" ControlStyle-Width="120px" ControlStyle-CssClass="auto-style188" ReadOnly="True"><ControlStyle  CssClass="auto-style188" Width="120px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="intDate" HeaderText="入仓时间" ControlStyle-Width="120px" ControlStyle-CssClass="auto-style188" ReadOnly="True"><ControlStyle  CssClass="auto-style188" Width="120px"></ControlStyle></asp:BoundField>

        <asp:BoundField DataField="pdays" HeaderText="实际生产日数" ControlStyle-Width="200px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="200px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="pltime" HeaderText="承诺交货日数" ControlStyle-Width="200px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="200px"></ControlStyle></asp:BoundField>
        <asp:BoundField DataField="RF012" HeaderText="备注" ControlStyle-Width="200px" ControlStyle-CssClass="auto-style188"> <ControlStyle CssClass="auto-style188" Width="200px"></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=RFID收单汇报" + 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.效果
浏览器的样式:
关于GridView和ASPxGridView导出Excel表格的解决方法
导出后用Excel打开:
关于GridView和ASPxGridView导出Excel表格的解决方法

  • ASPxGridView导出Excel
    待更新
相关标签: .NET