asp.net 导出GridView里的数据到Excel中,全部,当前页,选择行,选中行,所选行,复...

按下保存按钮,可以选择保存当前行,当前页和全部记录

#region 保存

/// <summary>
/// 保存
/// </summary>
protected void btnBC_Click(object sender, EventArgs e)
{
string save_cblJL = "";
for (int i = 0; i < this.cblJL.Items.Count; i++)
{
if (this.cblJL.Items[i].Selected == true)
{
save_cblJL += this.cblJL.Items[i].Value + ",";
}
}

string[] save_Excel = save_cblJL.Split(',');

for (int j = 0; j < save_Excel.Length - 1;j++ )
{
if (save_Excel[j].Equals("全部记录"))
{
toExcelClk(gvwjdccx, 3);
}
else if (save_Excel[j].Equals("当前页"))
{
toExcelClk(gvwjdccx, 2);
}
else if (save_Excel[j].Equals("当前记录"))
{
toExcelClk(gvwjdccx, 1);
}
}
}
#endregion 保存


保存Excel代码:
#region 导出为Excel

/// <summary>
/// 导出为Excel
/// </summary>
/// <param name="control">控件ID</param>
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}

/// <summary>
/// 导出为Excel
/// </summary>
/// <param name="ctl">控件ID</param>
/// <param name="FileName">文件名</param>
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}

/// <summary>
/// 导出为Excel
/// </summary>
/// <param name="ckbSelect">选择的复选框,1为导出当前选择行,2为导出当前页,3为导出所有记录</param>
private void toExcelClk(GridView gvw, int ckbSelect)
{
if (ckbSelect == 3)
{
gvw.AllowPaging = false;//关闭分页以导出所有记录
gvw.AllowSorting = false;
gvw.DataSource = dt;//绑定到数组
gvw.DataBind();
}

else if (ckbSelect == 1)
{
gvw.AllowPaging = false;
gvw.AllowSorting = false;
int i = -1;
foreach (GridViewRow gvwRow in this.gvw.Rows)
{
i++;
if (((CheckBox)gvwRow.FindControl("ckbSelect")).Checked)
{
gvw.Rows[i].Visible = true;
}
else
{
gvw.Rows[i].Visible = false;
}
}
}
gvw.Columns[17].Visible = false;//隐藏选择列,不导出选择列
ToExcel(gvw, "jdccx.xls");
gvw.AllowPaging = true;
gvw.AllowSorting = true;
gvw.Columns[17].Visible = true;//恢复选择列为可见
gvw.DataSource = dt;//绑定到数组
gvw.DataBind();
}

#endregion 导出为Excel


黑色头发:http://heisetoufa.iteye.com
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值