To render the gridview itself you can bind the data to it in code behind and then call the
RenderControl method. This provides you with the needed output of the rendered control which you can inject in a word template (see first link on how to do this).
these are used to capture the output of the rendering of the control. In code you explicitly call for the rendering of the databound control (so it has data), but instead of letting everything to get rendered to the page you capture the output in such a
writer and later on use that to push the outcome to the client.
Taken from MSDN: Writes markup characters and text to an ASP.NET server control output stream. This class provides formatting capabilities that ASP.NET server controls use when rendering markup
to clients.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Rameezwaheed
Contributor
3712 Points
1521 Posts
Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 11:26 AM|LINK
Hi, all
Is there any functionality in asp.net where i can export Gridview data into MS word by clicking on export button
Best Reagrds
Mark as an answer if it helps
XIII
All-Star
175978 Points
21429 Posts
ASPInsiders
Moderator
MVP
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 11:33 AM|LINK
Hi,
you can take this approach for example: How to use components of the .NET Framework 3.0 to create and then to stream an Office Word 2007 document and an Office Excel 2007 workbook to a client computer.
To render the gridview itself you can bind the data to it in code behind and then call the RenderControl method. This provides you with the needed output of the rendered control which you can inject in a word template (see first link on how to do this).
Another option, if you don't need some preformatted word template to merge with is to take a look at one of these aticles: http://search.live.com/results.aspx?q=asp.net+export+gridview+to+word&go=&form=QBLH. A lot is already written about this subject.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Ram Reddy Mekha
Star
9604 Points
1313 Posts
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 11:44 AM|LINK
Yes you can do like this...
protected void btnExportToDoc_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=Export.doc"); Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/vnd.word"; System.IO.StringWriter strWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter HtmltxtWriter = new System.Web.UI.HtmlTextWriter(strWriter); this.grdv1.RenderControl(HtmltxtWriter); Response.Output.Write(strWriter.ToString()); Response.Flush(); Response.End(); }where grdv1 is your gridviewAbhiram Reddy Mekha, +91-994-840-4315
Please "Mark as Answer" if the post helps you.
getchinna_sv
Star
12041 Points
2084 Posts
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 11:50 AM|LINK
vinayget
Member
254 Points
50 Posts
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 12:07 PM|LINK
hi
try buddy. It works fine
// page load event u can use other events
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=;Initial Catalog=;User ID=;Password=");
SqlCommand sqlCmd1 = new SqlCommand();
sqlCmd1.Connection = con;
sqlCmd1.CommandType = CommandType.Text;
sqlCmd1.CommandText = "select * from Table";
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlCmd1);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
Export ex = new Export();
ex.Convert(ds, Response);
}
}
// create a class say Export.cs
public class Export
{
public Export()
{
//
// TODO: Add constructor logic here
//
}
public void Convert(DataSet ds, HttpResponse response)
{
//*********Export to Excel/Doc**************
//first let's clean up the response.object
response.Clear();
response.Charset = "";
//set the response mime type for excel
// response.ContentType = "application/vnd.ms-excel"; // excel export
response.ContentType = "application/msword"; // doc export
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
//create an htmltextwriter which uses the stringwriter
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//instantiate a datagrid
DataGrid dg = new DataGrid();
//set the datagrid datasource to the dataset passed in
dg.DataSource = ds.Tables[0];
//bind the datagrid
dg.DataBind();
//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);
//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}
}
vinay
.net Sr. Developer
Hyderabad
Rameezwaheed
Contributor
3712 Points
1521 Posts
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 12:44 PM|LINK
Thanks to all for reply
what is use of stringwriter class, HtmlTextwriter class in above code can u clearfy me plz
Best Regards
Mark as an answer if it helps
XIII
All-Star
175978 Points
21429 Posts
ASPInsiders
Moderator
MVP
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 12:53 PM|LINK
Hi,
these are used to capture the output of the rendering of the control. In code you explicitly call for the rendering of the databound control (so it has data), but instead of letting everything to get rendered to the page you capture the output in such a writer and later on use that to push the outcome to the client.
Taken from MSDN: Writes markup characters and text to an ASP.NET server control output stream. This class provides formatting capabilities that ASP.NET server controls use when rendering markup to clients.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
mudassarkhan
All-Star
77546 Points
13188 Posts
MVP
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 21, 2009 01:06 PM|LINK
http://www.aspnettutorials.com/tutorials/file/gv-word-aspnet2-csharp.aspx
Contact me
Rameezwaheed
Contributor
3712 Points
1521 Posts
Re: Export Gridview Data to MS word document Is it Possible ?
Feb 22, 2009 06:52 AM|LINK
Thank you all for help
i got it working
Best Regards
Mark as an answer if it helps