

Navigator : Home > Tutorials > File Tutorials > ...
How to export GridView to Word using ASP.NET 2.0 and C#
This tutorial will show you how to export GridView to Word using ASP.NET 2.0 and C#.
This tutorial will show you how to export GridView to Word using ASP.NET 2.0 and C#.
First,you need to import the namespace from System.Data.SqlClient.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
| using System.Data.SqlClient; |
The System.Data.SqlClient namespace contains The System.Data.SqlClient namespace is the .NET Framework Data Provider for SQL Server.The .NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space. In tutorial, we need "AddHeader" and "ContentType" to do the work.The AddHeader method adds a new HTML header and value to the response sent to the client. It does not replace an existing header of the same name. After a header has been added, it cannot be removed. The ContentType property specifies the HTTP content type for the response. If no ContentType is specified, the default is text/HTML.
We use the Button1_Click event to do the work. We then call "Response.AddHeader" to export a file which is named FileName.doc. We then use "Response.ContentType" to denotes the type of the file being exported.
protected void Button1_Click(object sender, EventArgs e) {
Response.Clear(); Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/vnd.word"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); }
public override void VerifyRenderingInServerForm(Control control) {
} |
The front end GridViewExportWordVB.aspx page looks something like this:
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Export to Word" Width="99px" /><br /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> |
The flow for the code behind page is as follows.
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
string ConnectionString = "Data Source=(local);Initial Catalog=pubs;User Id=sa;Password=sa123"; SqlConnection cn1; protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
SqlConnection cn = new SqlConnection(ConnectionString); cn.Open(); cn1 = new SqlConnection(ConnectionString); cn1.Open(); SqlCommand cmd = new SqlCommand("select * from [authors]", cn); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = dr; GridView1.DataBind(); dr.Close(); cmd.Dispose(); cn.Dispose(); cn1.Dispose(); cn = cn1 = null; } } protected void Button1_Click(object sender, EventArgs e) {
Response.Clear(); Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/vnd.word"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } } |
Looking for the VB.NET2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!