RSS

Arquivo da categoria: Asp.Net MVC

Exportar DataGridView c# para Excel

using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Data.SqlClient;
	using System.Data;
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			SqlDataAdapter adapter = new SqlDataAdapter();
			DataSet ds = new DataSet();
			int i = 0;
			string sql = null;
			string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****";
			sql = "select * from publishers";
			SqlConnection connection = new SqlConnection(connetionString);
			connection.Open();
			SqlCommand command = new SqlCommand(sql, connection);
			adapter.SelectCommand = command;
			adapter.Fill(ds);
			connection.Close();
			GridView1.DataSource = ds.Tables[0];
			GridView1.DataBind();
		}
		protected void btntoExcel_Click(object sender, EventArgs e)
		{
			Response.ClearContent();
			Response.AddHeader("content-disposition", "attachment; filename=gvtoexcel.xls");
			Response.ContentType = "application/excel";
			System.IO.StringWriter sw = new System.IO.StringWriter();
			HtmlTextWriter htw = new HtmlTextWriter(sw);
			GridView1.RenderControl(htw);
			Response.Write(sw.ToString());
			Response.End();
		}
		public override void VerifyRenderingInServerForm(Control control)
		{
			/*Tell the compiler that the control is rendered
			 * explicitly by overriding the VerifyRenderingInServerForm event.*/
		}
	}

 

Anúncios
 
Deixe um comentário

Publicado por em 02/11/2017 em Asp.Net MVC

 

Tags: , , , , , , , , , , ,

Carregar uma partial view em uma div ASP.NET MVC

 type="text/javascript">
    $(document).ready(function () {
        $('#buttonId').click(function () {
            $('#divid').load('@Url.Action("About", "Home")');
        });
    });


<a id="buttonId">Load partial view or Any URL</a>
id="divid" style="height:100px; width:400px";>
 
Deixe um comentário

Publicado por em 18/01/2017 em Asp.Net MVC

 

Tags: , , , , , , , , , , ,