Export to Excel Sheet
Hello Guys.\ !
Today we'll make a program that how to convert GridView data in Excel Sheet..
so.........
just you have to make a GridView from your Toolbox along with a button..
nd just make it very easy code.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
//just make a connection with your database which table you want to use in your program.....
private void BindData()
{
string constr = @"Server=kishu-pc;Database=NORTHWIND;uid=sa; Password=pass@word1;";
string query = "SELECT ProductID, ProductName, UnitPrice FROM Products";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
// its simply use the Rendering your fille in Excel where it'll be download.......
}
// Take a button for converting the .doc file into the Excel file....
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=Products.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
// string simply write your doc file.....anlog with HtmlTextWriter....
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
GridView1.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
// used produts table from northwind database ....you can download northwind database from net.....
here is screenshot of the gridview
nd downloaded excelsheet is here..........
just enjoy with the program guys!.............
Today we'll make a program that how to convert GridView data in Excel Sheet..
so.........
just you have to make a GridView from your Toolbox along with a button..
nd just make it very easy code.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
//just make a connection with your database which table you want to use in your program.....
private void BindData()
{
string constr = @"Server=kishu-pc;Database=NORTHWIND;uid=sa; Password=pass@word1;";
string query = "SELECT ProductID, ProductName, UnitPrice FROM Products";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
// its simply use the Rendering your fille in Excel where it'll be download.......
}
// Take a button for converting the .doc file into the Excel file....
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=Products.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
// string simply write your doc file.....anlog with HtmlTextWriter....
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
GridView1.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
// used produts table from northwind database ....you can download northwind database from net.....
here is screenshot of the gridview
nd downloaded excelsheet is here..........
just enjoy with the program guys!.............
Comments
Post a Comment