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

#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