Monday 22 July 2013

How to Export Gridview Row or Record to the MSWord Document in Asp.Net



Description:-


In this Example we Explain That how to export Gridview Record to the Microsoft Word Document when Click on Export or Image of Word.Exporting to Word, Excel and CSV can be easily achieved using ASP.Net without any third party tools or export only selected or checked rows or records from gridview to Word or Excel sheet using asp.net.

This is very useful when we have to generally Create a Report or you can Directally print the Document. In this Example when user click on the Image of Word at that All Gridview Record are Exported to the Word Document with same Look and same color as in Gridview.

To Export GridView Record to MSWord First we Have to set the Content Type of the Page means to Display data in MSWord like



                        Response.ContentType = "application/ms-word";

you can also Export your gridview data to Excel or PDF to show this Example please click below Link



Export Gridview to Excel :- Gridview to Excel 

Change BackGround color of gridview when row is selected select row through Checkbox and change the color

to show example of Auto Complete TextBox AutoComplete TextBox in Ajax


word.aspx:-



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="word.aspx.cs" Inherits="word" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" id="tb" runat="server">
<tr>
<td align="right">

<asp:ImageButton ID="btnWord" runat="server" ImageUrl="~/WordImage.jpg"
onclick="btnWord_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView runat="server" ID="gvdetails"   AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="UserId" />
<asp:BoundField DataField="FirstName" HeaderText="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="age" HeaderText="age" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>




word.aspx.cs:-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class word : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            grid();
        }

    }
    public void grid()
    {
        DataSet ds = new DataSet();

        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";

        string q = "select * from tierword";

        SqlConnection conn = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(q, conn);



        SqlDataAdapter sa = new SqlDataAdapter();
        conn.Open();
        cmd.ExecuteNonQuery();
        sa.SelectCommand = cmd;
        sa.Fill(ds);
        gvdetails.DataSource = ds;
        gvdetails.DataBind();

    }
    protected void btnWord_Click(object sender, ImageClickEventArgs e)
    {
        gvdetails.AllowPaging = false;
        grid();
        gvdetails.DataBind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
        Response.Charset = "";
        Response.ContentType = "application/ms-word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gvdetails.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

        /* Verifies that the control is rendered */

    }
}
Email ThisBlogThis!Share to Twitter

0 comments:

Post a Comment