Hi PRA,
I have created sample code by referring the below article which full-fill your requirement. So please refer the below link.
HTML
<div>
<asp:Button ID="btnExportWord" runat="server" Text="ExportToWord" OnClick="btnExportWord_Click" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
this.GetData();
}
public DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Birthday");
dt.Columns.Add("Adress");
dt.Rows.Add("1", "Rustam", "22.12.1987", "District Shurobod");
dt.Rows.Add("2", "Firuz", "16.06.1988", "City Hisor");
dt.Rows.Add("3", "Asror", "06.02.1978", "City Kulob");
dt.Rows.Add("4", "Sadriddin", "08.12.1991", "City Dushanbe");
dt.Rows.Add("5", "Karomatullo", "07.05.1980", "City Dushanbe");
return dt;
}
protected void btnExportWord_Click(object sender, EventArgs e)
{
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = GetData();
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=DataTable.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Vb.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.GetData()
End Sub
Public Function GetData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("Id")
dt.Columns.Add("Name")
dt.Columns.Add("Birthday")
dt.Columns.Add("Adress")
dt.Rows.Add("1", "Rustam", "22.12.1987", "District Shurobod")
dt.Rows.Add("2", "Firuz", "16.06.1988", "City Hisor")
dt.Rows.Add("3", "Asror", "06.02.1978", "City Kulob")
dt.Rows.Add("4", "Sadriddin", "08.12.1991", "City Dushanbe")
dt.Rows.Add("5", "Karomatullo", "07.05.1980", "City Dushanbe")
Return dt
End Function
Protected Sub btnExportWord_Click(sender As Object, e As EventArgs)
'Create a dummy GridView
Dim GridView1 As New GridView()
GridView1.AllowPaging = False
GridView1.DataSource = GetData()
GridView1.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=DataTable.doc")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word "
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.[End]()
End Sub
Screenshot