ASP.NET GridView有选择的导出Excel

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

 

保存Excel

  

  1. #region 导出为Excel 
  2.  
  3. /// <summary> 
  4. /// 导出为Excel 
  5. /// </summary> 
  6. /// <param name="control">控件ID</param> 
  7. public override void VerifyRenderingInServerForm(Control control) 
  8.     // Confirms that an HtmlForm control is rendered for 
  9.  
  10. /// <summary> 
  11. /// 导出为Excel 
  12. /// </summary> 
  13. /// <param name="ctl">控件ID</param> 
  14. /// <param name="FileName">文件名</param> 
  15. private void ToExcel(Control ctl, string FileName) 
  16.     HttpContext.Current.Response.Charset = "UTF-8"
  17.     HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 
  18.     HttpContext.Current.Response.ContentType = "application/ms-excel"
  19.     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName); 
  20.     ctl.Page.EnableViewState = false
  21.     System.IO.StringWriter tw = new System.IO.StringWriter(); 
  22.     HtmlTextWriter hw = new HtmlTextWriter(tw); 
  23.     ctl.RenderControl(hw); 
  24.     HttpContext.Current.Response.Write(tw.ToString()); 
  25.     HttpContext.Current.Response.End(); 
  26.  
  27. /// <summary> 
  28. /// 导出为Excel 
  29. /// </summary> 
  30. /// <param name="ckbSelect">选择的复选框,1为导出当前选择行,2为导出当前页,3为导出所有记录</param> 
  31. private void toExcelClk(GridView gvw, int ckbSelect) 
  32.     if (ckbSelect == 3) 
  33.     { 
  34.         gvw.AllowPaging = false;//关闭分页以导出所有记录 
  35.         gvw.AllowSorting = false
  36.         gvw.DataSource = dt;//绑定到数组 
  37.         gvw.DataBind(); 
  38.     } 
  39.  
  40.     else if (ckbSelect == 1) 
  41.     { 
  42.         gvw.AllowPaging = false
  43.         gvw.AllowSorting = false
  44.         int i = -1; 
  45.         foreach (GridViewRow gvwRow in this.gvw.Rows) 
  46.         { 
  47.             i++; 
  48.             if (((CheckBox)gvwRow.FindControl("ckbSelect")).Checked) 
  49.             { 
  50.                 gvw.Rows[i].Visible = true
  51.             } 
  52.             else 
  53.             { 
  54.                 gvw.Rows[i].Visible = false
  55.             } 
  56.         } 
  57.     } 
  58.     gvw.Columns[17].Visible = false;//隐藏选择列,不导出选择列 
  59.     ToExcel(gvw, "jdccx.xls"); 
  60.     gvw.AllowPaging = true
  61.     gvw.AllowSorting = true
  62.     gvw.Columns[17].Visible = true;//恢复选择列为可见 
  63.     gvw.DataSource = dt;//绑定到数组 
  64.     gvw.DataBind(); 
  65. #endregion 导出为Excel 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值