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.
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 |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
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() End Sub 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() |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
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> |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
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 ' End Class 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 End Sub 'Page_LoadDim cn As New SqlConnection(ConnectionString) End Ifcn.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() Protected Sub Button1_Click(sender As Object, e As EventArgs) Response.Clear() End SubResponse.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() Public Overrides Sub VerifyRenderingInServerForm(control As Control) End Sub |
Looking for the C# 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
No comments:
Post a Comment