Hi
While click the button will display the word document from Grid View Datas. how to do that.
Pls send me with Examples.
Thanks&Regs
Magesh.A
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
jalpesh vadgama110 reputationPosted Mar 28, 2012, 10:33 PM EDT
http://www.codeproject.com/Articles/7093/One-click-ASP-NET-export-button-control-for-Excel
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 29, 2012, 4:23 AM EDT
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Export_to_word._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Export_to_word
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
bindgrid();
}
void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
g1.DataMember = "employee";
g1.DataSource = ds;
g1.DataBind();
con.Close();
}
protected void btn_export_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Gridviewdata.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
g1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
}
Thanks
If this post helps you mark it as answer
magesh manavalanRank #6,279 · 271 reputationPosted Mar 29, 2012, 8:17 PM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 29, 2012, 9:31 PM EDT
You just drag and drop a Gridview and rename the id="g2" or any name and change accordingly in the application.Don't copy paste the code into your .aspx file.
Thanks
magesh manavalanRank #6,279 · 271 reputationPosted Mar 29, 2012, 10:27 PM EDT