zoukankan      html  css  js  c++  java
  • GridView实用示例源码附加导出Excel功能

    代码

    代码
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Button2.Attributes[
    "onclick"] = "return slcNo_click();";
    GridViewBind(
    "");
    }
    private void GridViewBind(string Sqlsort)
    {
    string connStr = ConfigurationManager.ConnectionStrings["Conn51aspx"].ConnectionString;
    string SqlStr = "SELECT * FROM test01 where id<1000" + Sqlsort;
    DataSet ds
    = new DataSet();

    try
    {
    SqlConnection conn
    = new SqlConnection(connStr);
    if (conn.State.ToString() == "Closed") conn.Open();

    SqlDataAdapter da
    = new SqlDataAdapter(SqlStr, conn);
    da.Fill(ds,
    "test01");
    if (conn.State.ToString() == "Open") conn.Close();

    GridView1.DataSource
    = ds.Tables[0].DefaultView;
    GridView1.DataBind();

    LblCurrentIndex.Text
    = "" + (GridView1.PageIndex + 1).ToString() + "";
    LblPageCount.Text
    = "" + GridView1.PageCount.ToString() + "";
    LblRecordCount.Text
    = "总共 " + ds.Tables[0].Rows.Count.ToString() + "";
    if (ds.Tables[0].Rows.Count == 0)
    {
    btnFirst.Visible
    = false;
    btnPrev.Visible
    = false;
    btnNext.Visible
    = false;
    btnLast.Visible
    = false;

    LblCurrentIndex.Visible
    = false;
    LblPageCount.Visible
    = false;
    LblRecordCount.Visible
    = false;
    }
    else if (GridView1.PageCount == 1)
    {
    btnFirst.Visible
    = false;
    btnPrev.Visible
    = false;
    btnNext.Visible
    = false;
    btnLast.Visible
    = false;
    }

    // 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
    btnFirst.CommandName = "1";
    btnPrev.CommandName
    = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

    btnNext.CommandName
    = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
    btnLast.CommandName
    = GridView1.PageCount.ToString();
    //
    }
    catch (Exception ex)
    {
    Response.Write(
    "数据库错误,错误原因:" + ex.Message);
    Response.End();
    }
    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
    GridView1.PageIndex
    = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
    GridViewBind(
    "");
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    e.Row.Attributes[
    "onmouseover"] = "ItemOver(this)";

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    e.Row.Cells[
    1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
    }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    string str = "";
    string[] ckb = null;

    str
    = Request.Form.Get("checkboxname");
    ckb
    = str.Split(new char[] { ',' });

    Response.Write(
    "直接在页面中得到的值为:" + str + "<br>");

    Response.Write(
    "处理后存放在数组中,如下:<br>");
    for (int i = 0; i < ckb.Length; i++)
    {
    Response.Write(
    "ckb[" + i + "]的值为:" + ckb[i] + "<br>");
    }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write(Request.Form.Get(
    "RadioName"));
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
    string sql = "";

    if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
    {
    ViewState[
    "SortDirection"] = " desc";
    }
    else
    ViewState[
    "SortDirection"] = "";

    sql
    = " order by " + e.SortExpression + ViewState["SortDirection"];

    GridViewBind(sql);
    }

    //导出到Execl
    protected void Button3_Click(object sender, EventArgs e)
    {
    Response.ClearContent();
    Response.AddHeader(
    "content-disposition", "attachment; filename=MyExcelFile.xls");
    Response.ContentType
    = "application/excel";
    StringWriter sw
    = new StringWriter();
    HtmlTextWriter htw
    = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();


    }

    public override void VerifyRenderingInServerForm( Control control )
    { }


    }

     源代码

    https://files.cnblogs.com/lilo202/GridView%e5%ae%9e%e7%94%a8%e7%a4%ba%e4%be%8b%e6%ba%90%e7%a0%81%e9%99%84%e5%8a%a0%e5%af%bc%e5%87%baExcel%e5%8a%9f.rar

  • 相关阅读:
    树的直径
    POJ3264 Balanced Lineup
    [mock]10月11日
    POJ1062 昂贵的聘礼
    POJ3295 Tautology
    [topcoder]TopographicalImage
    POJ1753 Flip Game
    [leetcode]Copy List with Random Pointer
    [leetcode]Candy
    [leetcode]Gas Station
  • 原文地址:https://www.cnblogs.com/lilo202/p/1872482.html
Copyright © 2011-2022 走看看