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