Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to export DataTable to Word in ASP.NET

Author: Rachel Nicole
Download Source Code : 1344_ExportDataTableToWord.zip

The purpose of this article is to explain how we can export DataTable to Word.

 

I will get data in DataTable object from Customers table of NORTHWIND database and export this DataTable data into MS Word file. 

 

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Add a Button in Web Form

    <asp:Button ID="Button1" runat="server" Text="Export DataTable To Word File"
         onclick="Button1_Click" />
     
  6. Open code behind file and include following namespaces

    C#
     
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
     
     
    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.IO
     
  7. Write below function is code behind file to get data from NORTHWIND database

    C#
     
    protected DataTable GetProductsData()
    {
        string cmdText = "SELECT * FROM Products";
        string myConnection = "Data Source=Local;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True";
        SqlConnection connection = new SqlConnection(myConnection);
        SqlCommand command = new SqlCommand(cmdText, connection);
        connection.Open();
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        da.Fill(dt);
        connection.Close();
        return dt;
    }
     
     
    VB.NET
     
    Protected Function GetProductsData() As DataTable
        Dim cmdText As String = "SELECT * FROM Products"
        Dim myConnection As String = "Data Source=Local;Initial Catalog=C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF;Integrated Security=True"
        Dim connection As New SqlConnection(myConnection)
        Dim command As New SqlCommand(cmdText, connection)
        connection.Open()
        Dim da As New SqlDataAdapter(command)
        Dim dt As New DataTable()
        da.Fill(dt)
        connection.Close()
        Return dt
    End Function
     
    Here is the Explanation of above function
    I am using Products table in NORTHWIND database to get data.
    GetProductsData() function returns Datatable.
    Write query to get data from database table.
    Establish your connection to the server.
    Create SqlCommand object by passing query and SqlConnection object.
    Get data in SqlDataAdapter object.
    Create DataTable Object and fill DataTable.
    Return DataTable object.
     
  8. Write below function in code behind file to export DataTable to Word file

    C#
     
    protected void ExportDataTableToWord()
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word";
        Response.AddHeader("content-disposition", "attachment;filename=Products.doc");
     
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
     
        GridView GridView1 = new GridView();
        GridView1.DataSource = GetProductsData();
        GridView1.DataBind();
        GridView1.RenderControl(hWriter);
     
        Response.Write(sWriter.ToString());
        Response.End();
    }
     
    VB.NET
     
    Protected Sub ExportDataTableToWord()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.Buffer = True
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-word"
        Response.AddHeader("content-disposition", "attachment;filename=Products.doc")
     
        Dim sWriter As New StringWriter()
        Dim hWriter As New HtmlTextWriter(sWriter)
     
        Dim GridView1 As New GridView()
        GridView1.DataSource = GetProductsData()
        GridView1.DataBind()
        GridView1.RenderControl(hWriter)
     
        Response.Write(sWriter.ToString())
        Response.End()
    End Sub
     
    Here is the explanation of above function.
    First clear all content output and all headers from buffer stream.
    Set Buffer property of Response object to “True” and set the Charset property to empty.
    Set the ContentType property of Response object for MS Word.
    Call the AddHeader() method of Response object.
    Create a StringWrite object. Now create HtmlTexWrite object by passing StringWriter object as parameter. HtmlTexWrite object writes text to ASP.NET Server control output stream.
    I am creating a GridView control to bind DataTable to it.
    Now call the RenderControl() method of the server control which outputs the content of server control to provided HtmlTextWriter object.
    Call the Write() method of Response object by providing StringWriter object.
    End() method send all currently buffered output to the client and  stop execution.
     
  9. Write Button click event to call the function

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        ExportDataTableToWord();
    }
     
    VB.NET

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        ExportDataTableToWord()
    End Sub
     
  10. View web site in browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Encrypt and Decrypt text file in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net