Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - File - How to export GridView to Word using ASP.NET 2.0 and VB
How to export GridView to Word using ASP.NET 2.0 and VB


ASP.NET File Tutorial

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.

Untitled Document

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

This tutorial will show you how to export GridView to Word using ASP.NET 2.0 and VB.

First,you need to import the namespace from System.Data.SqlClient.

Imports 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 Sub Button1_Click(sender As Object, e As EventArgs)
Response.Clear()
Response.Buffer = True

Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")

Response.ContentEncoding = System.Text.Encoding.UTF7
Response.ContentType = "application/vnd.word"
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.GridView1.RenderControl(oHtmlTextWriter)
Response.Output.Write(oStringWriter.ToString())
Response.Flush()
Response.End()
End Sub

The front end  GridViewExportWordVB.aspx page looks something like this:

<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.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Text
Imports System.Data.SqlClient

Class _Default
Inherits System.Web.UI.Page '

Private ConnectionString As String = "Data Source=(local);Initial Catalog=pubs;User Id=sa;Password=sa123"
Private cn1 As SqlConnection

Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Dim cn As New SqlConnection(ConnectionString)
cn.Open()
cn1 = New SqlConnection(ConnectionString)
cn1.Open()
Dim cmd As New SqlCommand("select * from [authors]", cn)
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
GridView1.DataSource = dr
GridView1.DataBind()
dr.Close()
cmd.Dispose()
cn.Dispose()
cn1.Dispose()
End If
End Sub 'Page_Load

Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Clear()
Response.Buffer = True

Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")

Response.ContentEncoding = System.Text.Encoding.UTF7
Response.ContentType = "application/vnd.word"
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.GridView1.RenderControl(oHtmlTextWriter)
Response.Output.Write(oStringWriter.ToString())
Response.Flush()
Response.End()
End Sub

Public Overrides Sub VerifyRenderingInServerForm(control As Control)

End Sub
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks