• 转换GridView的内容到Excel里面 ---带有格式


    http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx

    1. HTML 页面

    添加GridView控件,编辑要绑定的字段。

    添加转换按钮

    <div>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                <SortedDescendingHeaderStyle BackColor="#93451F" />
    
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:BoundField DataField="Address" HeaderText="Address"  />
                </Columns>
            </asp:GridView>
    
            <asp:Button ID="Button1" runat="server" Text="ExportToExcel" OnClick="Button1_Click"/>
        </div>
    View Code

    2. 添加需要的命名空间

    using System.IO;
    using System.Data;
    using System.Drawing;
    using System.Data.SqlClient;
    using System.Configuration;

    3. 后台绑定GridView的方法

    private void BindGrid()
            {
                string strConnString = ConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                GridView1.DataSource = dt;
                                GridView1.DataBind();
                            }
                        }
                    }
                }
            }
    View Code

    4. 在页面加载的时候,绑定GridView

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGrid();
                }
            }
    View Code

    5. 实现GridView的分页--PageIndexChanging事件

     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView1.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
    View Code


    6.把GridView转换成Excel的方法

         protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-excel";
                using (StringWriter sw = new StringWriter())
                {
                    HtmlTextWriter hw = new HtmlTextWriter(sw);
    
                    //To Export all pages
                    GridView1.AllowPaging = false;
                    this.BindGrid();
    
                    GridView1.HeaderRow.BackColor = Color.White;
                    foreach (TableCell cell in GridView1.HeaderRow.Cells)
                    {
                        cell.BackColor = GridView1.HeaderStyle.BackColor;
                    }
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        row.BackColor = Color.White;
                       foreach (TableCell cell in row.Cells)
                        {
                            if (row.RowIndex % 2 == 0)
                            {
                                cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                            }
                            else
                            {
    
                                cell.BackColor = GridView1.RowStyle.BackColor
                            }
                            cell.CssClass = "textmode";
                        }
                    }
    
                    GridView1.RenderControl(hw);
                    //style to format numbers to string
                    string style = @"<style> .textmode { } </style>";
                    Response.Write(style);
                    Response.Output.Write(sw.ToString());
                    Response.Flush();
                    Response.End();
                }
            }
    View Code

    7. 必须添加的一个事件

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
  • 相关阅读:
    iOS面试题及答案大总结
    iOS 画音频波形曲线 根据音频数据版
    iPhone-获取网络数据或者路径的文件名
    python语言使用yaml 管理selenium元素
    出现事故后我们怎么复盘分析
    如何提升测试质量,减少漏测
    robotframework环境搭建
    如何做ui自动化---步骤详解
    Jenkins报错Caused: java.io.IOException: Cannot run program "sh" (in directory "D:JenkinsJenkins_homeworkspacejmeter_test"): CreateProcess error=2, 系统找不到指定的文件。
    使用jmeter使用Jenkins发送自定义消息内容
  • 原文地址:https://www.cnblogs.com/songxia/p/4063117.html
Copyright © 2020-2023  润新知