zoukankan      html  css  js  c++  java
  • asp.net 导出 gridview 数据 excel 全部 当前页 选择行 选中行 所选行 复选框

    按下保存按钮,可以选择保存当前行,当前页和全部记录
    1. #region 保存   
    2.   
    3. /// <summary>   
    4. /// 保存   
    5. /// </summary>   
    6. protected void btnBC_Click(object sender, EventArgs e)   
    7. {   
    8.     string save_cblJL = "";   
    9.     for (int i = 0; i < this.cblJL.Items.Count; i++)   
    10.     {   
    11.         if (this.cblJL.Items[i].Selected == true)   
    12.         {   
    13.             save_cblJL += this.cblJL.Items[i].Value + ",";   
    14.         }   
    15.     }   
    16.   
    17.     string[] save_Excel = save_cblJL.Split(',');   
    18.   
    19.     for (int j = 0; j < save_Excel.Length - 1;j++ )   
    20.     {   
    21.         if (save_Excel[j].Equals("全部记录"))   
    22.         {   
    23.             toExcelClk(gvwjdccx, 3);   
    24.         }   
    25.         else if (save_Excel[j].Equals("当前页"))   
    26.         {   
    27.             toExcelClk(gvwjdccx, 2);   
    28.         }   
    29.         else if (save_Excel[j].Equals("当前记录"))   
    30.         {   
    31.             toExcelClk(gvwjdccx, 1);   
    32.         }   
    33.     }   
    34. }  
    35. #endregion 保存  


    1. #region 导出为Excel   
    2.   
    3. /// <summary>   
    4. /// 导出为Excel   
    5. /// </summary>   
    6. /// <param name="control">控件ID</param>   
    7. public override void VerifyRenderingInServerForm(Control control)   
    8. {   
    9.     // Confirms that an HtmlForm control is rendered for   
    10. }   
    11.   
    12. /// <summary>   
    13. /// 导出为Excel   
    14. /// </summary>   
    15. /// <param name="ctl">控件ID</param>   
    16. /// <param name="FileName">文件名</param>   
    17. private void ToExcel(Control ctl, string FileName)   
    18. {   
    19.     HttpContext.Current.Response.Charset = "UTF-8";   
    20.     HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;   
    21.     HttpContext.Current.Response.ContentType = "application/ms-excel";   
    22.     HttpContext.Current.Response.AppendHeader("Content-Disposition""attachment;filename=" + "" + FileName);   
    23.     ctl.Page.EnableViewState = false;   
    24.     System.IO.StringWriter tw = new System.IO.StringWriter();   
    25.     HtmlTextWriter hw = new HtmlTextWriter(tw);   
    26.     ctl.RenderControl(hw);   
    27.     HttpContext.Current.Response.Write(tw.ToString());   
    28.     HttpContext.Current.Response.End();   
    29. }   
    30.   
    31. /// <summary>   
    32. /// 导出为Excel   
    33. /// </summary>   
    34. /// <param name="ckbSelect">选择的复选框,1为导出当前选择行,2为导出当前页,3为导出所有记录</param>   
    35. private void toExcelClk(GridView gvw, int ckbSelect)   
    36. {   
    37.     if (ckbSelect == 3)   
    38.     {   
    39.         gvw.AllowPaging = false;//关闭分页以导出所有记录   
    40.         gvw.AllowSorting = false;   
    41.         gvw.DataSource = dt;//绑定到数组   
    42.         gvw.DataBind();   
    43.     }   
    44.   
    45.     else if (ckbSelect == 1)   
    46.     {   
    47.         gvw.AllowPaging = false;   
    48.         gvw.AllowSorting = false;   
    49.         int i = -1;   
    50.         foreach (GridViewRow gvwRow in this.gvw.Rows)   
    51.         {   
    52.             i++;   
    53.             if (((CheckBox)gvwRow.FindControl("ckbSelect")).Checked)   
    54.             {   
    55.                 gvw.Rows[i].Visible = true;   
    56.             }   
    57.             else  
    58.             {   
    59.                 gvw.Rows[i].Visible = false;   
    60.             }   
    61.         }   
    62.     }   
    63.     gvw.Columns[17].Visible = false;//隐藏选择列,不导出选择列   
    64.     ToExcel(gvw, "jdccx.xls");   
    65.     gvw.AllowPaging = true;   
    66.     gvw.AllowSorting = true;   
    67.     gvw.Columns[17].Visible = true;//恢复选择列为可见   
    68.     gvw.DataSource = dt;//绑定到数组   
    69.     gvw.DataBind();   
    70. }  
    71.  
    72. #endregion 导出为Excel  
  • 相关阅读:
    主流配置中心的比较 Spring Cloud Config、Apollo、Nacos
    css进阶之路(一)----Web标准
    hello world
    服务过美国总统竞选的非传统投票UI [解析及DEMO]
    服务过美国总统竞选的非传统投票UI【demo已放出】
    大公司移动前端开发面试题——做转盘[参考代码]
    大公司移动前端开发面试题——做转盘[参考代码已放出]
    【前端杂谈】细数前端优化的化零为整
    前端开发的七宗罪
    CentOS7环境下在/离线安装GCC与GCC-C++
  • 原文地址:https://www.cnblogs.com/jordan2009/p/1553590.html
Copyright © 2011-2022 走看看